Advertisement
Guest User

Sim Grab

a guest
Oct 16th, 2019
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.23 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class SimHandGrab : MonoBehaviour
  6. {
  7.     public GameObject m_CollidingObject;
  8.     public GameObject m_HeldObject;
  9.  
  10.     private void OnTriggerStay(Collider other)
  11.     {
  12.         if(other.GetComponent<Rigidbody>())
  13.         {
  14.             m_CollidingObject = other.gameObject;
  15.         }
  16.     }
  17.     private void OnTriggerExit(Collider other)
  18.     {
  19.         m_CollidingObject = null;
  20.     }
  21.     // Update is called once per frame
  22.     void Update()
  23.     {
  24.         if(Input.GetKeyDown(KeyCode.Mouse1))
  25.         {
  26.             if(m_CollidingObject)
  27.             {
  28.                 Grab();
  29.             }
  30.         }
  31.         else if(Input.GetKeyUp(KeyCode.Mouse1))
  32.         {
  33.             if(m_HeldObject)
  34.             {
  35.                 Release();
  36.             }
  37.         }
  38.     }
  39.  
  40.     void Grab()
  41.     {
  42.         m_HeldObject = m_CollidingObject;
  43.         m_HeldObject.GetComponent<Rigidbody>().isKinematic = true;
  44.         m_HeldObject.transform.SetParent(transform);
  45.     }
  46.  
  47.     void Release()
  48.     {
  49.         m_HeldObject.transform.SetParent(null);
  50.         m_HeldObject.GetComponent<Rigidbody>().isKinematic = false;
  51.         m_HeldObject = null;
  52.     }
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement