Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- [RequireComponent(typeof(Rigidbody))]
- public class LeftHandScript : MonoBehaviour{
- [SerializeField] OVRInput.Controller Controller = OVRInput.Controller.LTouch;
- [SerializeField] Rigidbody AttachPoint = null;
- [SerializeField] Collider GrabCollider = null;
- [SerializeField] bool IgnoreContactPoint = false;
- Rigidbody mHeldObject;
- FixedJoint mTempJoint;
- Vector3 mOldVelocity;
- List<Rigidbody> mGrabCandidates;
- void Start() {
- mGrabCandidates = new List<Rigidbody>();
- if (AttachPoint == null)
- AttachPoint = GetComponent<Rigidbody>();
- if (GrabCollider == null)
- GrabCollider = GetComponent<Collider>();
- }
- void Update() {
- if(mHeldObject==null && OVRInput.Get(OVRInput.Axis1D.PrimaryIndexTrigger, Controller) >= 0.5f && mGrabCandidates.Count>0){
- //not holding, trigger down, potential grabbables
- Debug.Log("Grabbing "+mGrabCandidates[0]);
- mHeldObject = mGrabCandidates[0];
- mHeldObject.velocity = Vector3.zero;
- mTempJoint = mHeldObject.gameObject.AddComponent<FixedJoint>();
- mTempJoint.connectedBody = AttachPoint;
- GrabCollider.enabled = false;
- mGrabCandidates.Clear();
- }else if(mHeldObject!=null){
- if(OVRInput.Get(OVRInput.Axis1D.PrimaryIndexTrigger, Controller) < 0.5f){
- Debug.Log("Dropping "+mHeldObject);
- Object.Destroy(mTempJoint);
- mTempJoint = null;
- throwObject();
- mHeldObject = null;
- GrabCollider.enabled = true;
- }else{
- mOldVelocity = OVRInput.GetLocalControllerAngularVelocity(Controller);
- }
- }
- }
- void OnTriggerEnter(Collider collider){
- GameObject temp = collider.gameObject;
- if (temp != null && temp.layer == LayerMask.NameToLayer("grabbable") && temp.GetComponent<Rigidbody>() != null){
- mGrabCandidates.Add(temp.GetComponent<Rigidbody>());
- }
- }
- void OnTriggerExit(Collider collider){
- Rigidbody rbody = collider.gameObject.GetComponent<Rigidbody>();
- if(rbody!=null)
- mGrabCandidates.Remove(rbody);
- }
- void throwObject(){
- mHeldObject.velocity = OVRInput.GetLocalControllerVelocity(Controller);
- if (mOldVelocity != null)
- mHeldObject.angularVelocity = mOldVelocity;
- mHeldObject.maxAngularVelocity = mHeldObject.angularVelocity.magnitude;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement