玩家死后按下方向键仍然可以移动

Player can still move after death when dying while pressing directional keys

我目前正在使用 c# 学习 unity,并且我使用某人的教程制作了以下代码,但我认为我目前用作参考的教程并没有给我太多关于这个当前错误的信息。我已经添加了一个名为 dead 的布尔值,我将其放在 FixedUpdate() 和 Update() 上以检查是否接受输入。如果我不按方向键就死了,比如在攻击时死去和在空闲时死去,它会很好地工作……我想知道我的代码有什么问题。我会很高兴得到一些提示。这是我的 C# 代码。

PlayerManager.cs

using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class PlayerManager : MonoBehaviour
 {
     Rigidbody rb;
     public float x, y, ms = 3;
     Animator animator;
     public Collider weaponCollider;
     public PlayerUIManager playerUIManager;
     public int maxHp = 100;
     public int hp;
     bool dead;
  
     // Start is called before the first frame update
     void Start()
     {
         hp = maxHp;
         ms = 3;        
         playerUIManager.Init(this);
         rb = GetComponent<Rigidbody>();
         animator = GetComponent<Animator>();
         DisableWeaponCollider();
     }
 
     void Update()
     {
         if (dead) return;
         x = Input.GetAxisRaw("Horizontal");
         y = Input.GetAxisRaw("Vertical");
         if(Input.GetKeyDown(KeyCode.Space))
         {
             animator.SetTrigger("Attack");
         }
     }
 
     void GetDamage(int damage)
     {
         hp -= damage;
         if (hp <= 0)
         {
             hp = 0;
             dead = true;
             animator.SetTrigger("Die");
         }
         playerUIManager.UpdateHp(hp);
     }
 
     private void FixedUpdate()
     {
         if (dead) return;
         Debug.Log("fixed updating");
         Vector3 velocity = new Vector3(x, 0, y) * 0.29f;
         Vector3 direction = transform.position + velocity;
         transform.LookAt(direction);
         rb.velocity = velocity;
         animator.SetFloat("Speed", rb.velocity.magnitude);
     }
 
     private void OnTriggerEnter(Collider other)
     {
         if (dead) return;
         Damager damager = other.GetComponent<Damager>();
         if (damager != null)
         {
             animator.SetTrigger("DamageReceive");
             GetDamage(damager.damage);
         }
     }
 
     public void EnableWeaponCollider()
     {
         weaponCollider.enabled = true;
     }
 
     public void DisableWeaponCollider()
     {
         weaponCollider.enabled = false;
     }
 }

DieBehavior.cs(我的死亡动画)

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class DieBehavior : StateMachineBehaviour
 {
     override public void OnStateEnter(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
     {
         animator.GetComponent<PlayerManager>().ms = 0;
     }
 
     //override public void OnStateUpdate(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
     //{}
 
     //override public void OnStateExit(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
     //{}
 
     //override public void OnStateMove(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
     //{}
 
     //override public void OnStateIK(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
     //{}
 }
 

这个问题是因为 rb.velocity 仍然设置。

所以设置dead flag的时候需要一起重置velocity

if (hp <= 0)
{
     hp = 0;
     dead = true;
     animator.SetTrigger("Die");
     // Please add this
     rb.velocity = Vector3.zero;
}

Rigidbody.velocity 一次都不行。这意味着一旦你设置了速度,游戏对象就会移动,直到你重新设置速度。

Rigidbody.velocity表示Rigidbody位置的变化率