【问题标题】:How to Move and Jump in Unity如何在 Unity 中移动和跳跃
【发布时间】:2020-05-18 14:44:54
【问题描述】:

我正在制作第一人称射击游戏,目前正在研究跳跃。我正在使用动画跳跃,因为我之前一直在与重力作斗争。但是,我的问题是,如果我的跳跃脚本和移动一样附加到播放器上,我的播放器将无法移动。但是我知道这与脚本无关,因为两个脚本都可以工作,但不能同时工作。

我相信这与动画有关。动画有 3 个部分,Base、Running 和 Jump。如果玩家正在移动,则跑步动画处于活动状态,如果玩家按下空格键,则跳跃动画处于活动状态。跳转动画优先。

玩家不能移动和跳跃有什么原因吗?

这里是代码和动画控制器:

动画和跳跃

public class Jump : MonoBehaviour
{
    public bool run;
    public float speed;
    public GameObject player;
    public Animator dgbanim;
    public bool jump;
    public float rejump;
    public float rejumper;
    // Start is called before the first frame update
    void Start()
    {
        speed = 50;
        run = false;
        jump = false;
        rejumper = 0;
        rejump = 0;

    }

    // Update is called once per frame
    void Update()
    {

        if (jump == false && Input.GetKey("space")) { dgbanim.SetBool("Jump", true); jump = true; rejump = 21; }
        if (rejump > 0) { rejump = rejump - 1; }
        if(rejump == 0) { jump = false; dgbanim.SetBool("Jump", false); }

        if (Input.GetKey("w") || Input.GetKey("s") || Input.GetKey("a") || Input.GetKey("d") || Input.GetKey("up") || Input.GetKey("down") || Input.GetKey("left") || Input.GetKey("right")) { run = true; }


        if (run == true) { dgbanim.SetBool("Running", true); run = false; }
        else if (run == false) { dgbanim.SetBool("Running", false); }
    }

    private void OnCollisionEnter(Collision collision)
    {
        //if (collision.gameObject.tag != "EnemyBullets") { jump = true; rejump = 0; }
        if (collision.gameObject.tag != "EnemyBullets") { jump = false; }

    }
}

运动

public class Movement : MonoBehaviour
{
    public GameObject player;
    public float speed;
    // Start is called before the first frame update
    void Start()
    {
        speed = 50;
    }

    // Update is called once per frame
    void Update()
    {
        player.transform.Translate(Input.GetAxis("Horizontal") * speed * Time.deltaTime, 0, Input.GetAxis("Vertical") * speed * Time.deltaTime);
    }
}

【问题讨论】:

    标签: c# unity3d


    【解决方案1】:

    您的动画师似乎已选中“应用根动作”,并且它从动画而不是代码中获取动作,请尝试取消选中它。

    【讨论】:

      【解决方案2】:

      Animation 可以控制 GameObject 的一些属性,例如位置。如果您尝试在脚本中编辑游戏对象的位置,但动画控制了该位置,则动画将覆盖脚本对位置的更改。

      要查看这是否是问题所在,您可以打开动画窗口并选择您的播放器。如果玩家的位置(在检查器中)是红色的,那么动画正在控制它。

      我知道有两种方法可以解决这个问题:

      方法一(不推荐):编辑动画以移动根骨骼而不是播放器

      大多数角色模型都有一个名为“Armature”或“root”的游戏对象。你的角色的位置不应该在动画中。如果您需要在动画中向上移动角色,您可以为“Armature”或“根”游戏对象设置动画。在下面的屏幕截图中,请注意在动画窗口中您看不到“PlayerPref: position”,而是看到“Armature: position”。这允许我的移动脚本移动 PlayerPref 而不用动画覆盖位置。

      方法二(推荐):使用物理引擎跳转

      在您的动画中,不要让玩家跳跃。而是让玩家“原地跳跃”,即可能像跳跃一样向上摆动手臂,但实际上并没有向上移动。就像方法 1 一样,你的动画不应该设置玩家的位置。最简单的方法是在动画窗口中点击“玩家:位置”,然后按删除。

      为你的角色添加一个刚体。在您的脚本中:

      private Rigidbody rig;
      ...
      void Start(){
          rig = GetComponent<Rigidbody>();
      ...
      }
      

      在你触发跳跃的脚本中,你可以添加rig.AddForce(jumpPower * transform.up);。这将使物理系统处理跳跃。如果玩家正在跳跃但上面有东西,物理引擎将停止玩家。通过动画,玩家将跳过任何障碍。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-10-25
        • 1970-01-01
        • 2022-08-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多