using UnityEngine; //Player StayInPlace 动作-移动到停止时停下来 public class StayInPlace : MonoBehaviour { private InputHandler input; public Rigidbody rig; // Hip public float force;// 100 private Vector3 stopPosition; private bool isBroken; private PlayerDeath death; private Strength str; private float strength = 1f; private void Start() { str = GetComponent(); input = GetComponent(); death = GetComponent(); stopPosition = rig.position; } private void FixedUpdate() { if (death.dead) { return; } strength = str.strength; if (input.inputMovementDirection.magnitude > 0.1f) // 移动 { stopPosition = rig.position + rig.velocity * 0.25f; isBroken = false; } else if (!isBroken) // 移动到停下 { if (Vector3.Distance(stopPosition, rig.position) > 1f) { isBroken = true; } rig.AddForce((stopPosition - rig.position) * force * strength, ForceMode.Acceleration); } } }