Guest User

ChainHandle.cs

a guest
Jun 23rd, 2020
507
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.35 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.Events;
  5. using UnityEngine.UI;
  6.  
  7. public class ChainHandle : MonoBehaviour
  8. {
  9.     [SerializeField]
  10.     Transform player;
  11.     [SerializeField]
  12.     Transform firstChain;
  13.     float distance;
  14.     bool grabbed;
  15.     BoxCollider col;
  16.     Rigidbody rb;
  17.  
  18.     [SerializeField]
  19.     Text textPrompt;
  20.  
  21.     bool inRange;
  22.     // Start is called before the first frame update
  23.     void Start()
  24.     {          
  25.         distance = Vector3.Distance(transform.position, firstChain.position);
  26.         PlayerClamped.distance = distance;
  27.         col = GetComponent<BoxCollider>();
  28.         rb = GetComponent<Rigidbody>();
  29.         rb.isKinematic = false;
  30.         rb.useGravity = true;
  31.     }
  32.     void OnDrawGizmos()
  33.     {
  34.         Gizmos.color = Color.yellow;
  35.         if (firstChain != null)
  36.         {
  37.             Gizmos.DrawWireSphere(firstChain.position, distance) ;
  38.         }
  39.     }
  40.  
  41.     private void Update()
  42.     {
  43.         if (Input.GetKeyDown(KeyCode.E))
  44.         {
  45.             Grab();
  46.         }
  47.     }
  48.  
  49.     private void FixedUpdate()
  50.     {
  51.         if (grabbed)
  52.         {
  53.             // towards "player"
  54.             Vector3 dir = player.transform.position - firstChain.transform.position;
  55.             // clamp length
  56.             dir = Vector3.ClampMagnitude(dir, distance);
  57.             // add clamped length
  58.             transform.position = firstChain.transform.position + dir;
  59.             // look towards movement
  60.             transform.rotation = Quaternion.LookRotation(-dir);
  61.         }
  62.      
  63.     }
  64.  
  65.     void Grab()
  66.     {
  67.         grabbed = !grabbed;
  68.         col.enabled = !grabbed;
  69.         rb.isKinematic = grabbed;
  70.         rb.useGravity = !grabbed;
  71.         textPrompt.text = "Press E to drop";
  72.     }
  73.  
  74.     private void OnTriggerEnter(Collider other)
  75.     {
  76.         if(other.gameObject == player.gameObject)
  77.         {
  78.             inRange = true;
  79.             other.GetComponent<PlayerClamped>().inRange = true;
  80.             textPrompt.text = "Press E to grab";
  81.         }
  82.     }
  83.  
  84.     private void OnTriggerExit(Collider other)
  85.     {
  86.         if (other.gameObject == player.gameObject)
  87.         {
  88.             inRange = true;
  89.             other.GetComponent<PlayerClamped>().inRange = false;
  90.             textPrompt.text = "";
  91.  
  92.  
  93.         }
  94.     }
  95. }
Advertisement
Add Comment
Please, Sign In to add comment