mission-demolition/Assets/Scripts/Slingshot.cs
2024-02-22 01:23:52 -06:00

94 lines
3.5 KiB
C#

using UnityEngine;
using System.Collections;
public class Slingshot : MonoBehaviour
{
static private Slingshot s;
// fields set in the Unity Inspector pane
[Header("Set in Inspector")]
public GameObject prefabProjectile;
public float velocityMult = 8f;
public AudioClip stretchBandSound;
public AudioClip snapBandSound;
// fields set dynamically
[Header("Set Dynamically")]
public GameObject launchPoint;
public Vector3 launchPos;
public GameObject projectile;
public bool aimingMode;
private Rigidbody projectileRigidbody;
public AudioSource audioSource;
public float volume = 0.5f;
static public Vector3 LAUNCH_POS {
get {
if (s == null) return Vector3.zero;
return s.launchPos;
}
}
void Awake() {
s = this;
Transform launchPointTrans = transform.Find("LaunchPoint");
launchPoint = launchPointTrans.gameObject;
launchPoint.SetActive(false);
launchPos = launchPointTrans.position;
}
void OnMouseEnter() {
//print("Slingshot:OnMouseEnter()");
launchPoint.SetActive(true);
}
void OnMouseExit() {
//print("Slingshot:OnMouseExit()");
launchPoint.SetActive(false);
}
void OnMouseDown() {
// The player has pressed the mouse button while over Slingshot
aimingMode = true;
// Instantiate a projectile
projectile = Instantiate(prefabProjectile) as GameObject;
// Start it at the launchPoint
projectile.transform.position = launchPos;
// Set it to isKinematic for now
projectile.GetComponent<Rigidbody>().isKinematic = true;
// Set it to isKinematic for now
projectileRigidbody = projectile.GetComponent<Rigidbody>();
projectileRigidbody.isKinematic = true;
audioSource = GetComponent<AudioSource>();
audioSource.PlayOneShot(stretchBandSound, volume);
}
void Update() {
// If Slingshot is not in aimingMode, don't run this code
if (!aimingMode) return;
// Get the current mouse position in 2D screen coordinates
Vector3 mousePos2D = Input.mousePosition;
mousePos2D.z = -Camera.main.transform.position.z;
Vector3 mousePos3D = Camera.main.ScreenToWorldPoint(mousePos2D);
// Find the delta from the launchPos to the mousePos3D
Vector3 mouseDelta = mousePos3D - launchPos;
// Limit mouseDelta to the radius of the Slingshot SphereCollider
float maxMagnitude = this.GetComponent<SphereCollider>().radius;
if (mouseDelta.magnitude > maxMagnitude) {
mouseDelta.Normalize();
mouseDelta *= maxMagnitude;
}
// Move the projectile to this new position
Vector3 projPos = launchPos + mouseDelta;
projectile.transform.position = projPos;
// Get SlingshotBand and match point 1 with projectile
LineRenderer slingshotBand = s.GetComponentInChildren<LineRenderer>();
slingshotBand.SetPosition(1, mouseDelta);
if (Input.GetMouseButtonUp(0)) {
// The mouse has been released
aimingMode = false;
projectileRigidbody.isKinematic = false;
projectileRigidbody.velocity = -mouseDelta * velocityMult;
FollowCam.POI = projectile;
projectile = null;
MissionDemolition.ShotFired();
ProjectileLine.s.poi = projectile;
slingshotBand.SetPosition(1, new Vector3(0,0,0));
audioSource.PlayOneShot(snapBandSound, volume);
}
}
}