You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Unity2D添加Animator组件后玩家无法移动问题求助

Unity添加Animator后玩家无法移动的解决方案

玩家原本移动正常,添加动画后按下键盘按键玩家停止移动;移除Animator组件后移动恢复正常,已尝试分离动画与移动脚本,问题依旧。

相关代码

playerAnim 脚本

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class playerAnim : MonoBehaviour
{
    Animator anim;
    Rigidbody2D rb;
    void Start()
    {
        rb = gameObject.GetComponent<Rigidbody2D>();
        anim = gameObject.GetComponent<Animator>();
    }
    void FixedUpdate()
    {
        if (rb.velocity.x == 0)
            anim.SetBool("isRunning", false);
        else
            anim.SetBool("isRunning", true);
        if (rb.velocity.y > 0)
            anim.SetBool("isJumping", true);
        else
            anim.SetBool("isJumping", false);
    }
}

playerControl 脚本

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class playerControl : MonoBehaviour
{
    Rigidbody2D rb;
    private float horizontal;

    public float runSpeed;
    public float jumpPower;
    public bool inTheGround;
    private SpriteRenderer sp;
    Animator anim;

    void Start()
    {
        rb = gameObject.GetComponent<Rigidbody2D>();
        sp = gameObject.GetComponent<SpriteRenderer>();
        anim = gameObject.GetComponent<Animator>();
    }
    private void Update()
    {
        horizontal = Input.GetAxisRaw("Horizontal");
    }
    private void FixedUpdate()
    {
        rb.velocity = new Vector2(horizontal * runSpeed, rb.velocity.y);
        
        if (Input.GetButton("Jump") && inTheGround)
        {
            rb.velocity = new Vector2(rb.velocity.x, jumpPower);
        }
        flipping();
        Debug.Log(rb.velocity.x);
    }
    private void OnCollisionEnter2D(Collision2D other)
    {
        if (other.gameObject.CompareTag("ground"))
            inTheGround = true;
    }
    private void OnCollisionExit2D(Collision2D other)
    {
        if (other.gameObject.CompareTag("ground") && rb.velocity.y > 0.1)
            inTheGround = false;
    }
    void flipping()
    {
        if (Input.GetKey(KeyCode.RightArrow))
            sp.flipX = false;
        if (Input.GetKey(KeyCode.LeftArrow))
            sp.flipX = true;
    }
}

核心解决方法

这个问题绝大多数情况是Animator的Apply Root Motion选项导致的——开启该选项后,动画会直接控制物体位置,覆盖你通过Rigidbody编写的移动逻辑。

解决步骤:

  • 选中玩家对象,打开Inspector面板找到Animator组件
  • 取消勾选Apply Root Motion选项

额外优化建议

  1. 浮点精度修正:判断rb.velocity.x是否为0时,不要直接用==0,改成Mathf.Abs(rb.velocity.x) < 0.01f,避免微小速度值导致动画状态判断错误。修改后的playerAnim代码片段:
void FixedUpdate()
{
    if (Mathf.Abs(rb.velocity.x) < 0.01f)
        anim.SetBool("isRunning", false);
    else
        anim.SetBool("isRunning", true);
    // 跳跃判断逻辑不变
}
  1. 动画剪辑检查:如果取消Apply Root Motion后仍有问题,打开对应动画剪辑,在Inspector的Root Motion部分,确保Motion设置为None,避免动画自带的位移影响玩家移动。

内容的提问来源于stack exchange,提问作者ahmed

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.05 12:15:33