775bfd76ea
added more sprites, increased functionality, etc
142 lines
4.7 KiB
C#
142 lines
4.7 KiB
C#
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() {
|
|
m_camera = GameObject.FindGameObjectWithTag("MainCamera").GetComponent<Camera>();
|
|
grappleRope.enabled = false;
|
|
m_springJoint2D.enabled = false;
|
|
m_distanceJoint2D.distance = targetDistance + .5f;
|
|
m_distanceJoint2D.enabled = false;
|
|
inDistanceRange = false;
|
|
}
|
|
|
|
void Update() {
|
|
|
|
// Vector2 mousePos = m_camera.ScreenToWorldPoint(Mouse.current.position.ReadValue());
|
|
// RotateGun(mousePos, true);
|
|
|
|
if (grappleRope.isGrappling && !inDistanceRange && Vector2.Distance(grapplePoint, new Vector2(m_rigidBody2D.transform.position.x, m_rigidBody2D.transform.position.y)) < targetDistance) {
|
|
// print(Vector2.Distance(grapplePoint, new Vector2(m_rigidBody2D.transform.position.x, m_rigidBody2D.transform.position.y)) + ", target: " + targetDistance);
|
|
inDistanceRange = true;
|
|
}
|
|
if (grappleRope.isGrappling) {
|
|
RotateGun(grapplePoint, true);
|
|
}
|
|
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() {
|
|
grappleDistanceVector = grapplePoint - (Vector2)gunPivot.position;
|
|
// print("grapple");
|
|
m_springJoint2D.autoConfigureDistance = false;
|
|
m_distanceJoint2D.autoConfigureDistance = false;
|
|
|
|
m_springJoint2D.connectedAnchor = grapplePoint;
|
|
m_springJoint2D.enabled = true;
|
|
// print("Spring Joint Enabled");
|
|
// print("Sprint Joint Distance:" + m_springJoint2D.distance);
|
|
|
|
m_distanceJoint2D.connectedAnchor = grapplePoint;
|
|
}
|
|
|
|
public void GrappleToTambourine(GameObject tambourine) {
|
|
grappleDistanceVector = grapplePoint - (Vector2)gunPivot.position;
|
|
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) {
|
|
grappleDistanceVector = grapplePoint - (Vector2)gunPivot.position;
|
|
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;
|
|
gunPivot.rotation = Quaternion.AngleAxis(90, Vector3.forward);
|
|
// print("disabled");
|
|
}
|
|
|
|
}
|