mission-demolition/Assets/Scripts/Slingshot.cs

82 lines
3.0 KiB
C#
Raw Normal View History

2024-02-19 18:33:43 -06:00
using UnityEngine;
2024-02-19 18:39:28 -06:00
using System.Collections;
2024-02-19 18:33:43 -06:00
public class Slingshot : MonoBehaviour
{
2024-02-19 20:49:09 -06:00
static private Slingshot s;
2024-02-19 19:52:08 -06:00
// fields set in the Unity Inspector pane
[Header("Set in Inspector")]
public GameObject prefabProjectile;
public float velocityMult = 8f;
// fields set dynamically
[Header("Set Dynamically")]
public GameObject launchPoint;
public Vector3 launchPos;
public GameObject projectile;
public bool aimingMode;
private Rigidbody projectileRigidbody;
2024-02-19 20:49:09 -06:00
static public Vector3 LAUNCH_POS {
get {
if (s == null) return Vector3.zero;
return s.launchPos;
}
}
2024-02-19 19:52:08 -06:00
void Awake() {
2024-02-19 20:49:09 -06:00
s = this;
2024-02-19 19:52:08 -06:00
Transform launchPointTrans = transform.Find("LaunchPoint");
launchPoint = launchPointTrans.gameObject;
launchPoint.SetActive(false);
launchPos = launchPointTrans.position;
}
2024-02-19 18:39:28 -06:00
void OnMouseEnter() {
2024-02-19 19:52:08 -06:00
//print("Slingshot:OnMouseEnter()");
launchPoint.SetActive(true);
2024-02-19 18:33:43 -06:00
}
2024-02-19 18:39:28 -06:00
void OnMouseExit() {
2024-02-19 19:52:08 -06:00
//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;
}
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;
if (Input.GetMouseButtonUp(0)) {
// The mouse has been released
aimingMode = false;
projectileRigidbody.isKinematic = false;
projectileRigidbody.velocity = -mouseDelta * velocityMult;
FollowCam.POI = projectile;
projectile = null;
2024-02-19 21:26:17 -06:00
MissionDemolition.ShotFired();
ProjectileLine.s.poi = projectile;
2024-02-19 19:52:08 -06:00
}
2024-02-19 18:33:43 -06:00
}
}