unity3d Unity脚本中的运行速度未更改

bpzcxfmw  于 2022-11-16  发布在  其他
关注(0)|答案(2)|浏览(229)

我是一个新手,我正在尝试做一个简单的2D统一自上而下游戏,所以我做了一个玩家控制器脚本,试图让我的角色行走,奔跑和爬行。但在执行它的时候,我不能让我的角色在其他一切正常的情况下改变奔跑速度。

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>();
    }

    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()
    {
        //걷기
        if (Mathf.Approximately(movement.x, 0) && Mathf.Approximately(movement.y, 0))
        {
            animator.SetBool("isMove", false);
        }
        else
        {
            animator.SetBool("isMove", true);
        }

        //달리기
        if (Input.GetKey(KeyCode.LeftShift)){
            movementSpeed = runSpeed;
            animator.SetBool("isRun", true);
        }
        else{
            movementSpeed = normalSpeed;
            animator.SetBool("isRun", false);
        }

        //웅크리기
        if (Input.GetKey(KeyCode.LeftControl))
        {
            movementSpeed = crouchSpeed;
            animator.SetBool("isCrouch", true);
        }
        else
        {
            movementSpeed = normalSpeed;
            animator.SetBool("isCrouch", false);
        }

        //대기상태 x방향 설정
        if (movement.x > 0)
        {
            animator.SetFloat("horIdle", 1);
            animator.SetFloat("verIdle", 0);
        }
        else if (movement.x < 0)
        {
            animator.SetFloat("horIdle", -1);
            animator.SetFloat("verIdle", 0);
        }

        //대기상태 y방향 설정
        if (movement.y > 0)
        {
            animator.SetFloat("verIdle", 1);
            animator.SetFloat("horIdle", 0);
        }
        else if (movement.y < 0)
        {
            animator.SetFloat("verIdle", -1);
            animator.SetFloat("horIdle", 0);
        }

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

    
}

这是我写的代码为球员的momvement和我找不到任何问题在这里。

1tuwyuhd

1tuwyuhd1#

修改ifelse语句,检查用户输入,变为:

if (Input.GetKey(KeyCode.LeftShift))
{
    movementSpeed = runSpeed;
    animator.SetBool("isRun", true);
}
else if (Input.GetKey(KeyCode.LeftControl))
{
    movementSpeed = crouchSpeed;
    animator.SetBool("isCrouch", true);
}
else
{
    movementSpeed = normalSpeed;
    animator.SetBool("isRun", false);

    movementSpeed = normalSpeed;
    animator.SetBool("isCrouch", false);
}

您正在使用两个if-else语句来检查用户输入。对于每个if,都有一个else,因此当没有用户输入时,必须执行哪个else是不明确的。

zpgglvta

zpgglvta2#

我通常喜欢让我的if语句尽可能简单,这样,引入逻辑错误的可能性就更小。

public class PlayerController : MonoBehaviour
{
    [SerializeField] private float movementSpeed = 3.0f;
    [SerializeField] private float normalSpeed, runSpeed, crouchSpeed;

    private Vector2 movement;
    private SpriteRenderer rend;
    private Rigidbody2D rigidbody2D;

    Animator animator;

    void Start ( )
    {
        movement = Vector2.zero;
        rend = GetComponent<SpriteRenderer> ( );
        animator = GetComponent<Animator> ( );
        rigidbody2D = GetComponent<Rigidbody2D> ( );
    }

    private void FixedUpdate ( )
    {
        rigidbody2D.velocity = movement * movementSpeed;
    }

    private void Update ( )
    {
        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;

        //걷기
        animator.SetBool ( "isMove", movement != Vector2.zero );
        movementSpeed = normalSpeed;

        // We can check the key codes in one place, outside of any conditional code.
        // Note that both isRun and isCrouch at the same time might be a valid condition?
        var isCrouch = Input.GetKey ( KeyCode.LeftControl );
        var isRun = Input.GetKey ( KeyCode.LeftShift );
        // If run requires crouch to be false, then the following line checks for that:
        // var isRun = !isCrouch && Input.GetKey ( KeyCode.LeftShift );
        

        //달리기
        animator.SetBool ( "isRun", isRun );
        if ( isRun ) movementSpeed = runSpeed;

        //웅크리기
        // Crouch is checked after Run. This makes crouch speed override run speed if both keys are pressed.
        animator.SetBool ( "isCrouch", isCrouch );
        if ( isCrouch ) movementSpeed = crouchSpeed;

        // Here we assume that idle will be zero when there is no 'movement'
        Vector2 idle = Vector2.zero;

        // If there is movement then we update the idle value.
        //대기상태 x방향 설정
        if ( movement.x > 0 )
            idle.x = 1;
        else if ( movement.x < 0 )
            idle.x = -1;

        //대기상태 y방향 설정
        if ( movement.y > 0 )
            idle.y = 1;
        else if ( movement.y < 0 )
            idle.y = -1;

        animator.SetFloat ( "verIdle", idle.x );
        animator.SetFloat ( "horIdle", idle.y );

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

相关问题