imported grappling files

This commit is contained in:
slevy14
2023-04-11 19:55:24 -07:00
parent d85910fbb2
commit 5a1ab041de
54 changed files with 6156 additions and 3 deletions

View File

@@ -0,0 +1,30 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EnemyPatrol : MonoBehaviour {
public bool pinned = false;
public float range;
public float xLeft;
public float xRight;
public Vector2 movementVector = Vector2.right;
public float moveSpeed;
// Start is called before the first frame update
void Start() {
xLeft = transform.position.x - range;
xRight = transform.position.x + range;
movementVector *= moveSpeed;
}
// Update is called once per frame
void Update() {
if (!pinned) {
if (transform.position.x >= xRight || transform.position.x <= xLeft) {
movementVector = -movementVector;
}
transform.position += new Vector3(movementVector.x, 0, 0) * Time.deltaTime;
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: e136b6711a1f64a90b596e8a68b34b78
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

17
Assets/Scripts/Launch.cs Normal file
View File

@@ -0,0 +1,17 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Launch : MonoBehaviour {
[SerializeField] GameObject tambourine;
[SerializeField] private float horizSpeed;
[SerializeField] private float vertSpeed;
public void ThrowTambourine(int facing) {
GameObject newTambourine = Instantiate(tambourine, this.gameObject.transform.position, this.gameObject.transform.rotation);
// multiply horizSpeed by facing if not using moving launch point
newTambourine.GetComponent<Rigidbody2D>().AddForce(new Vector2(horizSpeed * facing, vertSpeed), ForceMode2D.Impulse);
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: b49ff353512cc43728014da636fce388
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,148 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;
public class PlayerBehavior : MonoBehaviour {
[Header("Physics:")]
public float moveSpeed;
public float jumpSpeed;
public float airSpeed;
private float _hInput;
private Rigidbody2D _rb;
private int forward = 1;
public PlayerInput playerInput;
public LayerMask groundLayer;
public Vector2 boxSize;
public float maxDistanceFromGround;
[Header("Tambourine:")]
[SerializeField] private Launch launcher;
[HideInInspector] public bool hasTambourine = true;
[Header("Grappling:")]
[SerializeField] public Tutorial_GrapplingGun grapplingGun;
[SerializeField] public Tutorial_GrapplingRope grapplingRope;
private GameObject grappleSurface;
void Start() {
_rb = GetComponent<Rigidbody2D>();
airSpeed = .5f * moveSpeed;
}
void Update() {
// move
// _hInput = Input.GetAxis("Horizontal");
// if (_hInput < 0) {
// forward = -1;
// } else if (_hInput > 0) {
// forward = 1;
// }
// jump
// if (Input.GetKeyDown(KeyCode.Space)) {
if (playerInput.actions["Jump"].WasPressedThisFrame() && IsGrounded()) {
_rb.AddForce(Vector2.up * jumpSpeed, ForceMode2D.Impulse);
}
// throw tambourine
// if (Input.GetKeyDown(KeyCode.K)) {
if (playerInput.actions["ThrowTambourine"].WasPressedThisFrame()) {
if (hasTambourine && !grapplingRope.isGrappling) {
launcher.ThrowTambourine(forward);
hasTambourine = false;
}
}
// grapple
GameObject tambourine = GameObject.FindGameObjectWithTag("tambourine");
// if (Input.GetKeyDown(KeyCode.L)) {
if (playerInput.actions["Grapple"].WasPressedThisFrame()) {
if (tambourine != null) { // grapple to tambourine
if (!grapplingRope.isGrappling && tambourine.GetComponent<TambourineBehavior>().pinned) {
grapplingGun.GrappleToTambourine(tambourine);
grapplingRope.isGrappling = true;
}
} else { // grapple to grapple surface
// RaycastHit2D hit = Physics2D.Raycast(transform.position, new Vector2(0.500f * forward, 0.866f));
// if (hit.collider != null) {
// print("hit " + hit.collider.gameObject.tag);
// if (hit.collider.gameObject.tag == "grappleSurface" && !grapplingRope.isGrappling) {
// grapplingGun.GrappleToSurface(hit.collider.gameObject.transform.position);
// grapplingRope.isGrappling = true;
// }
// else {print("missed");}
// }
if (grappleSurface != null) {
grapplingGun.GrappleToSurface(grappleSurface.transform.position);
grapplingRope.isGrappling = true;
}
}
}
// if (Input.GetKeyUp(KeyCode.L)) {
if (playerInput.actions["Grapple"].WasReleasedThisFrame()) {
if (tambourine != null && grapplingRope.isGrappling) {
tambourine.GetComponent<TambourineBehavior>().DestroySelf();
}
grapplingGun.ReleaseGrapple();
}
// if (Input.GetKey(KeyCode.L)) {
if (playerInput.actions["Grapple"].IsPressed()) {
Debug.DrawRay(transform.position, new Vector2(0.500f * forward, 0.866f), Color.green);
}
}
void OnMove(InputValue value) {
_hInput = value.Get<Vector2>().x;
if (_hInput < 0) {
forward = -1;
} else if (_hInput > 0) {
forward = 1;
}
print(_hInput);
}
void FixedUpdate() {
// if (_hInput != 0) {
// _rb.velocity = new Vector2(_hInput * moveSpeed, _rb.velocity.y);
// }
if (grapplingRope.isGrappling && _hInput != 0 && !IsGrounded()) {
// print("grappling force");
_rb.AddForce(new Vector2(_hInput * (airSpeed / 3), 0));
} else if (_hInput != 0 && !IsGrounded()) {
_rb.AddForce(new Vector2(_hInput * airSpeed, 0));
} else if (_hInput != 0) {
// print("normal movement");
_rb.AddForce(new Vector2(_hInput * moveSpeed, 0));
// _rb.velocity = new Vector2(_hInput * moveSpeed, _rb.velocity.y);
}
}
void OnTriggerEnter2D(Collider2D col) {
if (col.tag == "grappleSurface") {
grappleSurface = col.gameObject;
}
}
void OnTriggerExit2D(Collider2D col) {
if (col.tag == "grappleSurface") {
grappleSurface = null;
}
}
bool IsGrounded() {
if (Physics2D.BoxCast(transform.position, boxSize, 0, -transform.up, maxDistanceFromGround, groundLayer)) {
return true;
}
return false;
}
void OnDrawGizmos() {
Gizmos.color = Color.red;
Gizmos.DrawCube(transform.position-transform.up*maxDistanceFromGround,boxSize);
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: b0115312556794123a3cafad4b83d0a7
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,40 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;
public class ProgrammerHUDController : MonoBehaviour {
// assigned in inspector
[SerializeField] TMP_Text trumpetUI;
[SerializeField] TMP_Text clarinetUI;
[SerializeField] TMP_Text tambourineUI;
[SerializeField] TMP_Text cymbalUI;
[SerializeField] TMP_Text stateText;
[SerializeField] GameObject programmerHUD;
[SerializeField] StateController stateController;
// Start is called before the first frame update
void Start() {
trumpetUI.text = "Can Trumpet?\n" + stateController.canTrumpet;
clarinetUI.text = "Can Clarinet?\n" + stateController.canClarinet;
cymbalUI.text = "Can Cymbal?\n" + stateController.canCymbal;
tambourineUI.text = "Can Tambourine?\n" + stateController.canTambourine;
stateText.text = "Current State:\n" + stateController.currentState;
}
// Update is called once per frame
void Update() {
trumpetUI.text = "Can Trumpet?\n" + stateController.canTrumpet;
clarinetUI.text = "Can Clarinet?\n" + stateController.canClarinet;
cymbalUI.text = "Can Cymbal?\n" + stateController.canCymbal;
tambourineUI.text = "Can Tambourine?\n" + stateController.canTambourine;
stateText.text = "Current State:\n" + stateController.currentState;
if (Input.GetKeyDown(KeyCode.H)) {
programmerHUD.SetActive(!programmerHUD.activeSelf);
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 3318c04b8ab0d4465a73385a51fb6fa6
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,23 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ProjectileBehavior : MonoBehaviour {
public bool pinned = false;
public void Explode() {
Destroy(this.gameObject);
}
public void Pin() {
this.gameObject.GetComponent<Rigidbody2D>().constraints = RigidbodyConstraints2D.FreezeAll;
this.gameObject.GetComponent<BoxCollider2D>().enabled = false;
}
public void OnCollisionEnter2D(Collision2D col) {
if (col.gameObject.tag == "wall") {
Explode();
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 6fa3b9e074b394c7596834a8e04284d5
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,22 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ProjectileEnemy : MonoBehaviour {
[SerializeField] GameObject projectile;
[SerializeField] GameObject firePoint;
// Start is called before the first frame update
void Start() {
StartCoroutine(Fire());
}
IEnumerator Fire() {
yield return new WaitForSeconds(3f);
GameObject newProjectile = Instantiate(projectile, firePoint.transform.position, firePoint.transform.rotation);
newProjectile.GetComponent<Rigidbody2D>().AddRelativeForce(new Vector2(80, 0));
newProjectile.transform.Rotate(new Vector3(0,0,45));
StartCoroutine(Fire());
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 8457129b21c0d4a468efafbc12f2acc4
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,78 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class StateController : MonoBehaviour {
// this is mostly a placeholder for making the programmer HUD work properly
// something like this will be needed later but like with real variables
public bool canTrumpet = true;
public bool canTambourine = true;
public bool canClarinet = true;
public bool canCymbal = true;
public enum PlayerStates {
Idle,
Running,
Jumping,
Falling,
TrumpetJumping,
TambourineThrowing,
SpiderGrappling,
ClarinetDiving,
CymbalParrying,
}
public PlayerStates currentState = PlayerStates.Idle;
// Start is called before the first frame update
void Start() {
}
// Update is called once per frame
void Update() {
if (Input.GetKeyDown(KeyCode.Q)) {
canTrumpet = !canTrumpet;
print("Q");
}
if (Input.GetKeyDown(KeyCode.W)) {
canTambourine = !canTambourine;
}
if (Input.GetKeyDown(KeyCode.E)) {
canClarinet = !canClarinet;
}
if (Input.GetKeyDown(KeyCode.R)) {
canCymbal = !canCymbal;
}
if (Input.GetKeyDown("1")) {
currentState = PlayerStates.Idle;
}
if (Input.GetKeyDown("2")) {
currentState = PlayerStates.Running;
}
if (Input.GetKeyDown("3")) {
currentState = PlayerStates.Jumping;
}
if (Input.GetKeyDown("4")) {
currentState = PlayerStates.Falling;
}
if (Input.GetKeyDown("5")) {
currentState = PlayerStates.TrumpetJumping;
}
if (Input.GetKeyDown("6")) {
currentState = PlayerStates.TambourineThrowing;
}
if (Input.GetKeyDown("7")) {
currentState = PlayerStates.SpiderGrappling;
}
if (Input.GetKeyDown("8")) {
currentState = PlayerStates.ClarinetDiving;
}
if (Input.GetKeyDown("9")) {
currentState = PlayerStates.CymbalParrying;
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 73653b02e42804d288eaaf881c511225
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,86 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TambourineBehavior : MonoBehaviour {
private Rigidbody2D rb;
// private float timer;
private Animator animator;
private GameObject collidedObject;
private float timeLerped = 0.0f;
private float timeToLerp = 0.5f;
private GameObject player;
public bool pinned = false;
void Awake() {
rb = this.gameObject.GetComponent<Rigidbody2D>();
animator = this.gameObject.GetComponent<Animator>();
player = GameObject.FindGameObjectWithTag("Player");
}
void Start() {
// rb.AddForce(new Vector2(horizSpeed, vertSpeed), ForceMode2D.Impulse);
StartCoroutine(CheckToDestroy());
}
void Update() {
// if (Input.GetKeyUp(KeyCode.K)) {
// Destroy(this.gameObject);
// }
if (collidedObject != null && collidedObject.tag != "grappleSurface") {
rb.constraints = RigidbodyConstraints2D.FreezeAll;
// this.gameObject.transform.position = col.transform.position;
timeLerped += Time.deltaTime;
this.gameObject.transform.position = Vector2.Lerp(this.gameObject.transform.position, collidedObject.transform.position, timeLerped/timeToLerp);
if (this.gameObject.transform.position.x == collidedObject.transform.position.x && this.gameObject.transform.position.y == collidedObject.transform.position.y) {
animator.SetBool("pinned", true);
pinned = true;
} else {
// print("pinned, but not same position: " + this.gameObject.transform.position + " / " + collidedObject.transform.position);
}
}
}
void OnTriggerEnter2D(Collider2D col) {
collidedObject = col.gameObject;
this.gameObject.GetComponent<CircleCollider2D>().enabled = false;
if (collidedObject.tag == "Enemy") {
collidedObject.GetComponent<EnemyPatrol>().pinned = true;
} else if (collidedObject.tag == "Projectile") {
// print("pinned");
collidedObject.GetComponent<ProjectileBehavior>().Pin();
}
}
void OnCollisionEnter2D(Collision2D col) {
if (col.gameObject.tag == "wall") {
DestroySelf();
}
}
IEnumerator CheckToDestroy() {
yield return new WaitForSeconds(5f);
print("waited 5");
if (!player.GetComponent<PlayerBehavior>().grapplingRope.isGrappling) {
DestroySelf();
}
}
public void DestroySelf() {
if (collidedObject != null && collidedObject.tag == "Enemy") {
collidedObject.GetComponent<EnemyPatrol>().pinned = false;
} else if (collidedObject != null && collidedObject.tag == "Projectile") {
collidedObject.GetComponent<ProjectileBehavior>().Explode();
}
player.GetComponent<PlayerBehavior>().hasTambourine = true;
player.GetComponent<PlayerBehavior>().grapplingGun.ReleaseGrapple();
Destroy(this.gameObject);
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 4797d7ba2c50b4594bb88bfffee1f8d0
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,222 @@
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("Layers Settings:")]
[SerializeField] private bool grappleToAll = false;
[SerializeField] private int grappleLayerNumber = 9;
[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;
[Header("Distance:")]
[SerializeField] private bool hasMaxDistance = false;
[SerializeField] private float maxDistance = 20;
private enum LaunchType {
TransformLaunch,
PhysicsLaunch
}
[Header("Launching:")]
[SerializeField] private bool launchToPoint = true;
[SerializeField] private LaunchType launchType = LaunchType.PhysicsLaunch;
[SerializeField] private float launchSpeed = 1;
[Header("No Launch To Point")]
[SerializeField] private bool autoConfigureDistance = false;
[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() {
grappleRope.enabled = false;
m_springJoint2D.enabled = false;
m_distanceJoint2D.distance = targetDistance + .5f;
m_distanceJoint2D.enabled = false;
inDistanceRange = false;
}
void Update() {
// if (Input.GetKeyDown(KeyCode.Mouse0)) {
// SetGrapplePoint();
// } else if (Input.GetKey(KeyCode.Mouse0)) {
// if (grappleRope.enabled) {
// RotateGun(grapplePoint, false);
// } else {
// Vector2 mousePos = m_camera.ScreenToWorldPoint(Input.mousePosition);
// RotateGun(mousePos, true);
// }
// if (launchToPoint && grappleRope.isGrappling) {
// if (launchType == LaunchType.TransformLaunch) {
// Vector2 firePointDistance = firePoint.position - gunHolder.localPosition;
// Vector2 targetPos = grapplePoint - firePointDistance;
// gunHolder.position = Vector2.Lerp(gunHolder.position, targetPos, Time.deltaTime * launchSpeed);
// }
// }
// } else if (Input.GetKeyUp(KeyCode.Mouse0)) {
// ReleaseGrapple();
// } else {
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 (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);
}
}
// void SetGrapplePoint() {
// Vector2 distanceVector = m_camera.ScreenToWorldPoint(Input.mousePosition) - gunPivot.position;
// // print("clicked" + m_camera.ScreenToWorldPoint(Input.mousePosition));
// // print("distance vector: " + distanceVector);
// if (Physics2D.Raycast(firePoint.position, distanceVector.normalized)) {
// RaycastHit2D _hit = Physics2D.Raycast(firePoint.position, distanceVector.normalized);
// print(_hit.transform.gameObject.name);
// if (_hit.transform.gameObject.layer == grappleLayerNumber || grappleToAll) {
// if (Vector2.Distance(_hit.point, firePoint.position) <= maxDistance || !hasMaxDistance) {
// // print("gunPivot " + gunPivot.position + ", grappling to: " + _hit.point);
// grapplePoint = _hit.point;
// grappleDistanceVector = grapplePoint - (Vector2)gunPivot.position;
// grappleRope.enabled = true;
// }
// }
// }
// }
public void Grapple() {
print("grapple");
m_springJoint2D.autoConfigureDistance = false;
m_distanceJoint2D.autoConfigureDistance = false;
if(!launchToPoint && !autoConfigureDistance) {
m_springJoint2D.distance = targetDistance;
print("Sprint Joint Distance:" + m_springJoint2D.distance);
m_springJoint2D.frequency = targetFrequency;
}
if (!launchToPoint) {
if (autoConfigureDistance) {
m_springJoint2D.connectedAnchor = grapplePoint;
m_springJoint2D.enabled = true;
// print("Spring Joint Enabled");
print("Sprint Joint Distance:" + m_springJoint2D.distance);
m_distanceJoint2D.connectedAnchor = grapplePoint;
}
} else {
switch (launchType) {
case LaunchType.PhysicsLaunch:
m_springJoint2D.connectedAnchor = grapplePoint;
m_distanceJoint2D.connectedAnchor = grapplePoint;
Vector2 distanceVector = firePoint.position - gunHolder.position;
m_springJoint2D.distance = distanceVector.magnitude;
m_springJoint2D.frequency = launchSpeed;
m_springJoint2D.enabled = true;
// m_distanceJoint2D.maxDistanceOnly = false;
m_distanceJoint2D.distance = targetDistance + .5f;
break;
case LaunchType.TransformLaunch:
m_rigidBody2D.gravityScale = 0;
m_rigidBody2D.velocity = Vector2.zero;
break;
}
}
}
public void GrappleToTambourine(GameObject tambourine) {
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) {
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;
// print("disabled");
}
private void OnDrawGizmosSelected() {
if (firePoint != null && hasMaxDistance) {
Gizmos.color = Color.green;
Gizmos.DrawWireSphere(firePoint.position, maxDistance);
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 6e8c21a2613bf4423a2f49601a3310e3
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,104 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Tutorial_GrapplingRope : MonoBehaviour {
[Header("General References:")]
public Tutorial_GrapplingGun grapplingGun;
public LineRenderer m_lineRenderer;
[Header("General Settings:")]
[SerializeField] private int precision = 40;
[Range(0, 20)] private float straightenLineSpeed = 5;
[Header("Rope Animation Settings:")]
public AnimationCurve ropeAnimationCurve;
[Range(0.1f, 4)] [SerializeField] private float startWaveSize = 2;
float waveSize = 0;
[Header("Rope Progression:")]
public AnimationCurve ropeProgressionCurve;
[SerializeField] [Range(1,50)] private float ropeProgressionSpeed = 1;
float moveTime = 0;
[HideInInspector] public bool isGrappling = true;
bool straightLine = false;
private void OnEnable() {
// print("on enabled called");
moveTime = 0;
m_lineRenderer.positionCount = precision;
waveSize = startWaveSize;
straightLine = false;
LinePointsToFirePoint();
m_lineRenderer.enabled = true;
}
private void OnDisable() {
// print("on disabled called");
m_lineRenderer.enabled = false;
isGrappling = false;
}
private void LinePointsToFirePoint() {
for (int i = 0; i < precision; i++) {
m_lineRenderer.SetPosition(i, grapplingGun.firePoint.position);
}
}
void Update() {
moveTime += Time.deltaTime;
DrawRope();
}
void DrawRope() {
// print("drawing");
// print("isGrappling: " + isGrappling);
if (!straightLine) {
float roundedLinePos = Mathf.Round(m_lineRenderer.GetPosition(precision - 1).x * 10.0f) * .01f;
float roundedGrapplePos = Mathf.Round(m_lineRenderer.GetPosition(precision - 1).x * 10.0f) * .01f;
print(roundedLinePos + " / " + roundedGrapplePos);
if (roundedLinePos == roundedGrapplePos) {
straightLine = true;
} else {
DrawRopeWaves();
}
} else {
if (!isGrappling) {
grapplingGun.Grapple();
isGrappling = true;
}
if (waveSize > 0) {
waveSize -= Time.deltaTime * straightenLineSpeed;
DrawRopeWaves();
} else {
waveSize = 0;
if (m_lineRenderer.positionCount != 2) {
m_lineRenderer.positionCount = 2;
}
DrawRopeNoWaves();
}
}
}
void DrawRopeWaves() {
for (int i = 0; i < precision; i++) {
float delta = (float)i / ((float)precision - 1f);
Vector2 offset = Vector2.Perpendicular(grapplingGun.grappleDistanceVector).normalized * ropeAnimationCurve.Evaluate(delta) * waveSize;
Vector2 targetPosition = Vector2.Lerp(grapplingGun.firePoint.position, grapplingGun.grapplePoint, delta) + offset;
Vector2 currentPosition = Vector2.Lerp(grapplingGun.firePoint.position, targetPosition, ropeProgressionCurve.Evaluate(moveTime) * ropeProgressionSpeed);
m_lineRenderer.SetPosition(i, currentPosition);
}
}
void DrawRopeNoWaves() {
m_lineRenderer.SetPosition(0, grapplingGun.firePoint.position);
m_lineRenderer.SetPosition(1, grapplingGun.grapplePoint);
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 06317215013e44d0fb7902fbc1c5e84e
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant: