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

Unity 2D俯视角游戏角色跑步速度无法更改问题求助

问题分析与修复方案

问题根源

你的代码里,跑步(LeftShift)和爬行(LeftControl)的逻辑判断顺序有问题,导致跑步速度被覆盖:

  • 按下LeftShift时,movementSpeed被设为runSpeed,但紧接着的爬行判断里,因为没按LeftControl,会执行else分支把movementSpeed改回normalSpeed,所以跑步速度永远不生效。
  • 如果同时按两个键,爬行逻辑在后面,会直接覆盖跑步的速度设置。

修复后的代码

调整逻辑顺序,优先处理爬行,再判断跑步,同时增加状态互斥:

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

public class PlayerController : MonoBehaviour
{
    SpriteRenderer rend;
    public float movementSpeed = 3.0f;
    Vector2 movement = new Vector2();
    Rigidbody2D rigidbody2D;
    [SerializeField] float normalSpeed, runSpeed, crouchSpeed;

    Animator animator;

    void Start()
    {
        rend = GetComponent<SpriteRenderer>();
        animator = GetComponent<Animator>();
        rigidbody2D = GetComponent<Rigidbody2D>();
        // 初始化速度为正常行走速度
        movementSpeed = normalSpeed;
    }

    void Update()
    {
        UpdateState();
    }

    private void FixedUpdate()
    {
        MoveCharacter();
    }

    private void MoveCharacter()
    {
        movement.x = Input.GetAxisRaw("Horizontal");
        movement.y = Input.GetAxisRaw("Vertical");

        if (movement.x > 0)
        {
            rend.flipX = true;
        }
        else if (movement.x < 0)
        {
            rend.flipX = false;
        }
        
        rigidbody2D.velocity = movement * movementSpeed;
    }

    private void UpdateState()
    {
        // 判断是否在移动
        bool isMoving = !Mathf.Approximately(movement.x, 0) || !Mathf.Approximately(movement.y, 0);
        animator.SetBool("isMove", isMoving);

        // 优先处理爬行(爬行优先级高于跑步)
        if (Input.GetKey(KeyCode.LeftControl))
        {
            movementSpeed = crouchSpeed;
            animator.SetBool("isCrouch", true);
            animator.SetBool("isRun", false); // 爬行时取消跑步状态
        }
        // 没爬行时,判断是否跑步(可选:只有移动时才触发跑步)
        else if (Input.GetKey(KeyCode.LeftShift) && isMoving)
        {
            movementSpeed = runSpeed;
            animator.SetBool("isRun", true);
            animator.SetBool("isCrouch", false);
        }
        // 既不爬也不跑,恢复正常速度
        else
        {
            movementSpeed = normalSpeed;
            animator.SetBool("isRun", false);
            animator.SetBool("isCrouch", false);
        }

        // 待机方向设置:移动时更新最后朝向,停止后保持
        if (isMoving)
        {
            if (Mathf.Abs(movement.x) > Mathf.Abs(movement.y))
            {
                animator.SetFloat("horIdle", movement.x);
                animator.SetFloat("verIdle", 0);
            }
            else
            {
                animator.SetFloat("verIdle", movement.y);
                animator.SetFloat("horIdle", 0);
            }
        }

        animator.SetFloat("horizontal", movement.x);
        animator.SetFloat("vertical", movement.y);
    }
}

关键修改说明

  • 调整判断顺序:先处理爬行,再处理跑步,避免后执行的逻辑覆盖前面的速度设置。
  • 状态互斥:爬行时强制关闭跑步动画,跑步时关闭爬行动画,防止状态混乱。
  • 优化待机方向:只在移动时更新待机方向,确保角色停下后保持最后移动的朝向。
  • 初始化速度:在Start里把movementSpeed设为normalSpeed,避免初始值异常。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.14 08:55:18