Advertisement
mew_fi

grapple

Oct 5th, 2016
275
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.18 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class p_grapple : MonoBehaviour {
  5.  
  6.     [SerializeField]
  7.     private LayerMask layerMask;
  8.     private Transform _transform;
  9.     private Rigidbody _body;
  10.  
  11.     public Rigidbody hookBody;
  12.  
  13.     private float defaultAngularDrag;
  14.  
  15.     public GameObject hookPoint;
  16.     public ConfigurableJoint conjoint;
  17.     [SerializeField]
  18.     private float range;
  19.     private float distance, hookAngle;
  20.     private p_movements pmove;
  21.     private float oldroll;
  22.     public float shootdelay;
  23.     private float nextshoot;
  24.  
  25.     public float frictionNormal, frictionHooked, bounceNormal, bounceHooked, swingRoll;
  26.  
  27.     private RaycastHit hit;
  28.     public LineRenderer rope;
  29.  
  30.     public Renderer inRange, outRange;
  31.  
  32.     [HideInInspector]
  33.     public bool canHit, canHook = true, isHooked;
  34.  
  35.     public AudioSource audio_hook;
  36.  
  37.     void Start() {
  38.         _transform = GetComponent<Transform>();
  39.         _body = GetComponent<Rigidbody>();
  40.         pmove = GetComponent<p_movements>();
  41.         defaultAngularDrag = _body.angularDrag;
  42.         nextshoot = 0;
  43.  
  44.         pmove.pmat.dynamicFriction = frictionNormal;
  45.         pmove.pmat.staticFriction = frictionNormal;
  46.         pmove.pmat.bounciness = bounceNormal;
  47.         oldroll = pmove.m_RollPower;
  48.         canHook = true;
  49.     }
  50.  
  51.     void Update () {
  52.         Ray ray = new Ray(Camera.main.transform.position, Camera.main.transform.forward);
  53.         if (Physics.Raycast(ray, out hit, 100f, layerMask.value) && hit.transform.gameObject.layer != 11)
  54.             distance = hit.distance; //old = Vector3.Distance(_transform.position, hit.point);
  55.         else
  56.             distance = 100.0f;
  57.  
  58.         hookAngle = Vector3.Angle(Camera.main.transform.forward, (hit.point - _transform.position));
  59.         if (distance > range || hookAngle > 100) {
  60.             outRange.enabled = true;
  61.             inRange.enabled = false;
  62.             canHit = false;
  63.         } else if (distance <= range && hookAngle <= 100) {
  64.             outRange.enabled = false;
  65.             inRange.enabled = true;
  66.             canHit = true;
  67.         }
  68.         if (Input.GetButton("Hook") && !Input.GetButton("Jump") && !Input.GetButton("Dash")
  69.             && Time.timeScale > 0 && canHit && canHook && !hookPoint.gameObject.activeInHierarchy) {
  70.             audio_hook.Play();
  71.             hookPoint.transform.position = hit.point;
  72.             hookPoint.gameObject.SetActive(true);
  73.  
  74.             if (hit.rigidbody != null && !hit.rigidbody.isKinematic)
  75.                 conjoint.connectedBody = hit.rigidbody;
  76.  
  77.             _body.angularDrag = 0;
  78.             pmove.pmat.bounciness = bounceHooked;
  79.             pmove.m_RollPower *= swingRoll;
  80.             pmove.pmat.dynamicFriction = frictionHooked;
  81.             pmove.pmat.staticFriction = frictionHooked;
  82.                    
  83.             nextshoot = Time.time + shootdelay;
  84.             isHooked = true;
  85.             canHook = false;
  86.         }
  87.  
  88.         if (Input.GetButtonUp("Hook")) {
  89.             Invoke("UnGrapple", 0);
  90.             canHook = true;
  91.         }
  92.  
  93.         if (hookPoint.activeInHierarchy) {
  94.             rope.SetPosition(0, hookPoint.transform.position);
  95.             rope.SetPosition(1, _transform.position);
  96.         }
  97.     }
  98.     void UnGrapple() {
  99.         hookBody.angularVelocity = Vector3.zero;
  100.         hookPoint.gameObject.SetActive(false);
  101.  
  102.         _body.angularDrag = defaultAngularDrag;
  103.  
  104.         pmove.m_RollPower = oldroll;
  105.  
  106.         pmove.pmat.bounciness = bounceNormal;
  107.         pmove.pmat.dynamicFriction = frictionNormal;
  108.         pmove.pmat.staticFriction = frictionNormal;
  109.  
  110.         if (conjoint.connectedBody != null) conjoint.connectedBody = null;
  111.         isHooked = false;
  112.     }
  113. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement