2023-04-11 19:55:24 -07:00
|
|
|
using System.Collections;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using UnityEngine;
|
|
|
|
using UnityEngine.InputSystem;
|
|
|
|
|
|
|
|
public class Tutorial_GrapplingGun : MonoBehaviour {
|
|
|
|
|
|
|
|
[Header("Scripts References:")]
|
|
|
|
public Tutorial_GrapplingRope grappleRope;
|
|
|
|
|
|
|
|
[Header("Main Camera:")]
|
|
|
|
public Camera m_camera;
|
|
|
|
|
|
|
|
[Header("Transform References:")]
|
|
|
|
public Transform gunHolder;
|
|
|
|
public Transform gunPivot;
|
|
|
|
public Transform firePoint;
|
|
|
|
|
|
|
|
[Header("Physics References:")]
|
|
|
|
public SpringJoint2D m_springJoint2D;
|
|
|
|
public DistanceJoint2D m_distanceJoint2D;
|
|
|
|
public Rigidbody2D m_rigidBody2D;
|
|
|
|
|
|
|
|
[Header("Rotation:")]
|
|
|
|
[SerializeField] private bool rotateOverTime = true;
|
|
|
|
[Range(0, 60)][SerializeField] private float rotationSpeed = 4;
|
|
|
|
|
|
|
|
private enum LaunchType {
|
|
|
|
TransformLaunch,
|
|
|
|
PhysicsLaunch
|
|
|
|
}
|
|
|
|
|
|
|
|
[Header("No Launch To Point")]
|
|
|
|
[SerializeField] private float targetDistance = 3;
|
|
|
|
[SerializeField] private float targetFrequency = 1;
|
|
|
|
|
|
|
|
[HideInInspector] public Vector2 grapplePoint;
|
|
|
|
[HideInInspector] public Vector2 grappleDistanceVector;
|
|
|
|
|
|
|
|
[HideInInspector] public bool inDistanceRange = false;
|
|
|
|
|
|
|
|
|
|
|
|
void Start() {
|
2023-04-13 16:52:11 -07:00
|
|
|
m_camera = GameObject.FindGameObjectWithTag("MainCamera").GetComponent<Camera>();
|
2023-04-11 19:55:24 -07:00
|
|
|
grappleRope.enabled = false;
|
|
|
|
m_springJoint2D.enabled = false;
|
|
|
|
m_distanceJoint2D.distance = targetDistance + .5f;
|
|
|
|
m_distanceJoint2D.enabled = false;
|
|
|
|
inDistanceRange = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Update() {
|
2023-04-17 16:47:12 -07:00
|
|
|
|
2023-04-19 17:24:55 -07:00
|
|
|
// Vector2 mousePos = m_camera.ScreenToWorldPoint(Mouse.current.position.ReadValue());
|
|
|
|
// RotateGun(mousePos, true);
|
2023-04-11 19:55:24 -07:00
|
|
|
|
|
|
|
if (grappleRope.isGrappling && !inDistanceRange && Vector2.Distance(grapplePoint, new Vector2(m_rigidBody2D.transform.position.x, m_rigidBody2D.transform.position.y)) < targetDistance) {
|
2023-04-17 16:47:12 -07:00
|
|
|
// print(Vector2.Distance(grapplePoint, new Vector2(m_rigidBody2D.transform.position.x, m_rigidBody2D.transform.position.y)) + ", target: " + targetDistance);
|
2023-04-11 19:55:24 -07:00
|
|
|
inDistanceRange = true;
|
|
|
|
}
|
2023-04-19 17:24:55 -07:00
|
|
|
if (grappleRope.isGrappling) {
|
|
|
|
RotateGun(grapplePoint, true);
|
|
|
|
}
|
2023-04-11 19:55:24 -07:00
|
|
|
if (inDistanceRange) {
|
|
|
|
m_distanceJoint2D.enabled = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void RotateGun(Vector3 lookPoint, bool allowRotationOverTime) {
|
|
|
|
Vector3 distanceVector = lookPoint - gunPivot.position;
|
|
|
|
float angle = Mathf.Atan2(distanceVector.y, distanceVector.x) * Mathf.Rad2Deg;
|
|
|
|
|
|
|
|
if (rotateOverTime && allowRotationOverTime) {
|
|
|
|
gunPivot.rotation = Quaternion.Lerp(gunPivot.rotation, Quaternion.AngleAxis(angle, Vector3.forward), Time.deltaTime * rotationSpeed);
|
|
|
|
} else {
|
|
|
|
gunPivot.rotation = Quaternion.AngleAxis(angle, Vector3.forward);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
public void Grapple() {
|
2023-04-19 17:24:55 -07:00
|
|
|
grappleDistanceVector = grapplePoint - (Vector2)gunPivot.position;
|
2023-04-17 16:47:12 -07:00
|
|
|
// print("grapple");
|
2023-04-11 19:55:24 -07:00
|
|
|
m_springJoint2D.autoConfigureDistance = false;
|
|
|
|
m_distanceJoint2D.autoConfigureDistance = false;
|
|
|
|
|
2023-04-17 16:47:12 -07:00
|
|
|
m_springJoint2D.connectedAnchor = grapplePoint;
|
|
|
|
m_springJoint2D.enabled = true;
|
|
|
|
// print("Spring Joint Enabled");
|
|
|
|
// print("Sprint Joint Distance:" + m_springJoint2D.distance);
|
|
|
|
|
|
|
|
m_distanceJoint2D.connectedAnchor = grapplePoint;
|
2023-04-11 19:55:24 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
public void GrappleToTambourine(GameObject tambourine) {
|
2023-04-19 17:24:55 -07:00
|
|
|
grappleDistanceVector = grapplePoint - (Vector2)gunPivot.position;
|
2023-04-11 19:55:24 -07:00
|
|
|
grappleRope.enabled = true;
|
|
|
|
|
|
|
|
grapplePoint = tambourine.transform.position;
|
|
|
|
|
|
|
|
m_springJoint2D.autoConfigureDistance = false;
|
|
|
|
m_distanceJoint2D.autoConfigureDistance = false;
|
|
|
|
|
|
|
|
m_springJoint2D.frequency = targetFrequency;
|
|
|
|
|
|
|
|
m_springJoint2D.connectedAnchor = grapplePoint;
|
|
|
|
m_springJoint2D.enabled = true;
|
|
|
|
// print("Spring Joint Enabled");
|
|
|
|
|
|
|
|
m_distanceJoint2D.connectedAnchor = grapplePoint;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void GrappleToSurface(Vector2 surfacePoint) {
|
2023-04-19 17:24:55 -07:00
|
|
|
grappleDistanceVector = grapplePoint - (Vector2)gunPivot.position;
|
2023-04-11 19:55:24 -07:00
|
|
|
grappleRope.enabled = true;
|
|
|
|
|
|
|
|
grapplePoint = surfacePoint;
|
|
|
|
|
|
|
|
m_springJoint2D.autoConfigureDistance = false;
|
|
|
|
m_distanceJoint2D.autoConfigureDistance = false;
|
|
|
|
|
|
|
|
m_springJoint2D.frequency = targetFrequency;
|
|
|
|
|
|
|
|
m_springJoint2D.connectedAnchor = grapplePoint;
|
|
|
|
m_springJoint2D.enabled = true;
|
|
|
|
// print("Spring Joint Enabled");
|
|
|
|
|
|
|
|
m_distanceJoint2D.connectedAnchor = grapplePoint;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void ReleaseGrapple() {
|
|
|
|
grappleRope.enabled = false;
|
|
|
|
m_springJoint2D.enabled = false;
|
|
|
|
m_distanceJoint2D.enabled = false;
|
|
|
|
inDistanceRange = false;
|
|
|
|
m_rigidBody2D.gravityScale = 1;
|
2023-04-20 13:31:08 -07:00
|
|
|
gunPivot.rotation = Quaternion.AngleAxis(90, Vector3.forward);
|
2023-04-11 19:55:24 -07:00
|
|
|
// print("disabled");
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|