Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- /*
- * This class is attached to a door handle. The door handle is child of a door.
- */
- public class PhysicsOpenDoor : MonoBehaviour
- {
- [SerializeField] private Rigidbody door;
- [SerializeField] private Vector3 force;
- [SerializeField] private Vector3 cross;
- [SerializeField] private bool holdingHandle;
- [SerializeField] private float angle;
- private const float forceMultiplier = 1500f;
- private void OnTriggerStay(Collider coll)
- {
- Debug.Log("Trigger Stay");
- Transform controller = coll.transform;
- if (Input.GetAxis(controller.name + " Grip Squeeze") != 0)
- {
- Debug.Log("Grip Pressed");
- holdingHandle = true;
- // Direction vector from the door's pivot point to the hand's current position
- Vector3 doorPivotToHand = controller.transform.position - transform.parent.position;
- // Ignore the y axis of the direction vector
- // doorPivotToHand.y = 0;
- // Direction vector from door handle to hand's current position
- force = controller.transform.position - transform.position;
- // Cross product between force and direction.
- cross = Vector3.Cross(doorPivotToHand, force);
- angle = Vector3.Angle(doorPivotToHand, force);
- }
- else
- {
- holdingHandle = false;
- }
- }
- void Update()
- {
- if (holdingHandle)
- {
- // Apply cross product and calculated angle to
- door.angularVelocity = cross * angle * forceMultiplier;
- }
- }
- private void OnTriggerExit()
- {
- // Set angular velocity to zero if the hand stops hovering
- door.angularVelocity = Vector3.zero;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement