Advertisement
Guest User

grabbing

a guest
Nov 21st, 2019
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.73 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5.  
  6. [RequireComponent(typeof(Rigidbody))]
  7. public class LeftHandScript : MonoBehaviour{
  8.  
  9. [SerializeField] OVRInput.Controller Controller = OVRInput.Controller.LTouch;
  10. [SerializeField] Rigidbody AttachPoint = null;
  11. [SerializeField] Collider GrabCollider = null;
  12. [SerializeField] bool IgnoreContactPoint = false;
  13.  
  14. Rigidbody mHeldObject;
  15. FixedJoint mTempJoint;
  16. Vector3 mOldVelocity;
  17. List<Rigidbody> mGrabCandidates;
  18.  
  19. void Start() {
  20. mGrabCandidates = new List<Rigidbody>();
  21. if (AttachPoint == null)
  22. AttachPoint = GetComponent<Rigidbody>();
  23. if (GrabCollider == null)
  24. GrabCollider = GetComponent<Collider>();
  25. }
  26.  
  27. void Update() {
  28.  
  29. if(mHeldObject==null && OVRInput.Get(OVRInput.Axis1D.PrimaryIndexTrigger, Controller) >= 0.5f && mGrabCandidates.Count>0){
  30. //not holding, trigger down, potential grabbables
  31. Debug.Log("Grabbing "+mGrabCandidates[0]);
  32. mHeldObject = mGrabCandidates[0];
  33. mHeldObject.velocity = Vector3.zero;
  34. mTempJoint = mHeldObject.gameObject.AddComponent<FixedJoint>();
  35. mTempJoint.connectedBody = AttachPoint;
  36. GrabCollider.enabled = false;
  37. mGrabCandidates.Clear();
  38.  
  39. }else if(mHeldObject!=null){
  40.  
  41. if(OVRInput.Get(OVRInput.Axis1D.PrimaryIndexTrigger, Controller) < 0.5f){
  42. Debug.Log("Dropping "+mHeldObject);
  43. Object.Destroy(mTempJoint);
  44. mTempJoint = null;
  45. throwObject();
  46. mHeldObject = null;
  47. GrabCollider.enabled = true;
  48. }else{
  49. mOldVelocity = OVRInput.GetLocalControllerAngularVelocity(Controller);
  50. }
  51. }
  52. }
  53.  
  54. void OnTriggerEnter(Collider collider){
  55.  
  56. GameObject temp = collider.gameObject;
  57. if (temp != null && temp.layer == LayerMask.NameToLayer("grabbable") && temp.GetComponent<Rigidbody>() != null){
  58. mGrabCandidates.Add(temp.GetComponent<Rigidbody>());
  59. }
  60. }
  61.  
  62. void OnTriggerExit(Collider collider){
  63. Rigidbody rbody = collider.gameObject.GetComponent<Rigidbody>();
  64. if(rbody!=null)
  65. mGrabCandidates.Remove(rbody);
  66. }
  67.  
  68. void throwObject(){
  69. mHeldObject.velocity = OVRInput.GetLocalControllerVelocity(Controller);
  70. if (mOldVelocity != null)
  71. mHeldObject.angularVelocity = mOldVelocity;
  72.  
  73. mHeldObject.maxAngularVelocity = mHeldObject.angularVelocity.magnitude;
  74. }
  75.  
  76.  
  77.  
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement