首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在我的统一脚本中,运行速度没有改变

在我的统一脚本中,运行速度没有改变
EN

Stack Overflow用户
提问于 2022-11-03 03:09:27
回答 2查看 62关注 0票数 0

我是一个初学者,我试图做一个简单的统一自上而下的游戏在2d,所以我做了一个玩家控制器脚本,并试图让我的角色行走,运行,爬行。但是在执行时,我不能让我的角色在其他一切正常工作的情况下改变运行速度。

代码语言:javascript
复制
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);
    }

    
}

这是我为玩家的进步写的代码,我在这里找不到任何问题。

EN

回答 2

Stack Overflow用户

发布于 2022-11-03 05:16:10

将检查用户输入的if else语句修改为:

代码语言:javascript
复制
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是不明确的。

票数 1
EN

Stack Overflow用户

发布于 2022-11-03 09:09:49

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

代码语言:javascript
复制
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 );
    }
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/74297775

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档