Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class SimHandGrab : MonoBehaviour
- {
- public GameObject m_CollidingObject;
- public GameObject m_HeldObject;
- private void OnTriggerStay(Collider other)
- {
- if(other.GetComponent<Rigidbody>())
- {
- m_CollidingObject = other.gameObject;
- }
- }
- private void OnTriggerExit(Collider other)
- {
- m_CollidingObject = null;
- }
- // Update is called once per frame
- void Update()
- {
- if(Input.GetKeyDown(KeyCode.Mouse1))
- {
- if(m_CollidingObject)
- {
- Grab();
- }
- }
- else if(Input.GetKeyUp(KeyCode.Mouse1))
- {
- if(m_HeldObject)
- {
- Release();
- }
- }
- }
- void Grab()
- {
- m_HeldObject = m_CollidingObject;
- m_HeldObject.GetComponent<Rigidbody>().isKinematic = true;
- m_HeldObject.transform.SetParent(transform);
- }
- void Release()
- {
- m_HeldObject.transform.SetParent(null);
- m_HeldObject.GetComponent<Rigidbody>().isKinematic = false;
- m_HeldObject = null;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement