From 6ce8b9e22fc13be34b442c7b6af48b42cd44275a Mon Sep 17 00:00:00 2001 From: chai <215380520@qq.com> Date: Wed, 13 Mar 2024 11:00:58 +0800 Subject: +init --- AddForceToTarget.cs | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 AddForceToTarget.cs (limited to 'AddForceToTarget.cs') diff --git a/AddForceToTarget.cs b/AddForceToTarget.cs new file mode 100644 index 0000000..9464c97 --- /dev/null +++ b/AddForceToTarget.cs @@ -0,0 +1,26 @@ +using UnityEngine; + +public class AddForceToTarget : MonoBehaviour +{ + public AnimationCurve forceByRangeCurve; + + public Rigidbody target; + + public float force; + + private Transform head; + + private void Start() + { + head = GetComponentInChildren().transform; + } + + private void FixedUpdate() + { + float num = forceByRangeCurve.Evaluate(Vector3.Distance(head.position, target.position)) * force; + if (num > 0f) + { + target.AddForce((head.position - target.position).normalized * num, ForceMode.Force); + } + } +} -- cgit v1.1-26-g67d0