diff options
author | chai <215380520@qq.com> | 2024-03-13 11:00:58 +0800 |
---|---|---|
committer | chai <215380520@qq.com> | 2024-03-13 11:00:58 +0800 |
commit | 6ce8b9e22fc13be34b442c7b6af48b42cd44275a (patch) | |
tree | b38119d2acf0a982cb67e381f146924b9bfc3b3f /HoldableObject.cs |
+init
Diffstat (limited to 'HoldableObject.cs')
-rw-r--r-- | HoldableObject.cs | 88 |
1 files changed, 88 insertions, 0 deletions
diff --git a/HoldableObject.cs b/HoldableObject.cs new file mode 100644 index 0000000..3bfbfc5 --- /dev/null +++ b/HoldableObject.cs @@ -0,0 +1,88 @@ +using UnityEngine; + +public class HoldableObject : MonoBehaviour +{ + public Transform holder; + + public Vector3 holdingPosition; + + public Vector3 holdingRotation; + + public Vector3 holdingUpRotation; + + [HideInInspector] + public Rigidbody rig; + + [HideInInspector] + public Transform leftHandPos; + + [HideInInspector] + public Transform rightHandPos; + + [HideInInspector] + public PID pid; + + [HideInInspector] + public Vector2 swingAngles; + + public float swingSpring; + + public Vector2 twistAngles; + + public float twistSpring; + + public bool isHeld; + + private DragHandler dragHandler; + + public LayerMask mask; + + private void Awake() + { + pid = GetComponent<PID>(); + rig = GetComponent<Rigidbody>(); + RightHandPos componentInChildren = GetComponentInChildren<RightHandPos>(); + if ((bool)componentInChildren) + { + rightHandPos = componentInChildren.transform; + } + LeftHandPos componentInChildren2 = GetComponentInChildren<LeftHandPos>(); + if ((bool)componentInChildren2) + { + leftHandPos = componentInChildren2.transform; + } + dragHandler = GetComponent<DragHandler>(); + mask = LayerMask.GetMask("Map"); + } + + private void Update() + { + if (isHeld) + { + dragHandler.dragAmount = 1f; + } + else + { + dragHandler.dragAmount = 0f; + } + } + + private void FixedUpdate() + { + if (!isHeld) + { + Hover(); + } + } + + private void Hover() + { + RaycastHit hitInfo = default(RaycastHit); + Ray ray = new Ray(base.transform.position, Vector3.down); + Physics.Raycast(ray, out hitInfo, 1.5f, mask); + Vector3 vector = new Vector3(0f, 0f, 0f); + vector.y = 1f - hitInfo.distance; + rig.velocity = Vector3.Lerp(rig.velocity, vector * 2f, Time.fixedDeltaTime * 100f); + rig.angularVelocity = Vector3.Lerp(rig.angularVelocity, Vector3.up * 5f, Time.deltaTime * 100f); + } +} |