Advertisement
Guest User

grabber

a guest
Nov 21st, 2019
225
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.04 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.  
  30. if(mHeldObject==null && OVRInput.Get(OVRInput.Axis1D.PrimaryIndexTrigger, Controller) >= 0.7f && mGrabCandidates.Count>0){
  31. // if(mHeldObject==null && Input.GetKey(KeyCode.Space) && mGrabCandidates.Count>0){
  32. //not holding, trigger down, potential grabbables
  33. Debug.Log("Grabbing "+mGrabCandidates[0]);
  34. mHeldObject = mGrabCandidates[0];
  35. mHeldObject.velocity = Vector3.zero;
  36. mTempJoint = mHeldObject.gameObject.AddComponent<FixedJoint>();
  37. mTempJoint.connectedBody = AttachPoint;
  38. GrabCollider.enabled = false;
  39. mGrabCandidates.Clear();
  40.  
  41. }else if(mHeldObject!=null){
  42.  
  43. if(OVRInput.Get(OVRInput.Axis1D.PrimaryIndexTrigger, Controller) < 0.3f){
  44. // if(Input.GetKey(KeyCode.Escape)){
  45. Debug.Log("Dropping "+mHeldObject);
  46. mTempJoint = null;
  47. Object.Destroy(mTempJoint);
  48. throwObject();
  49. mHeldObject = null;
  50. GrabCollider.enabled = true;
  51. }else{
  52. mOldVelocity = OVRInput.GetLocalControllerAngularVelocity(Controller);
  53. }
  54. }
  55. }
  56.  
  57. void OnTriggerEnter(Collider collider){
  58. if(collider.gameObject.name=="Sphere"){
  59. GameObject temp = collider.gameObject;
  60. if (temp != null && temp.layer == LayerMask.NameToLayer("grabbable") && temp.GetComponent<Rigidbody>() != null){
  61. mGrabCandidates.Add(temp.GetComponent<Rigidbody>());
  62. }
  63. }
  64.  
  65. }
  66.  
  67. void OnTriggerExit(Collider collider){
  68. if(collider.gameObject.name=="Sphere"){
  69. Rigidbody rbody = collider.gameObject.GetComponent<Rigidbody>();
  70. if(rbody!=null)
  71. mGrabCandidates.Remove(rbody);
  72. }
  73. }
  74.  
  75. void throwObject(){
  76. mHeldObject.velocity = OVRInput.GetLocalControllerVelocity(Controller);
  77. if (mOldVelocity != null)
  78. mHeldObject.angularVelocity = mOldVelocity;
  79.  
  80. mHeldObject.maxAngularVelocity = mHeldObject.angularVelocity.magnitude;
  81. }
  82.  
  83.  
  84.  
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement