【问题标题】:If two WASD keys are pressed, the character moves in the direction of the first keypress - but continues in that direction once it has been lifted如果按下两个 WASD 键,角色会朝第一次按键的方向移动 - 但一旦抬起,角色就会继续朝那个方向移动
【发布时间】:2021-07-29 00:08:27
【问题描述】:

我是 Unity 新手,所以请善待

所以我希望我的角色朝着最后按下的键的方向移动。如果我同时按下 W(上)和 D(右),玩家将朝最先出现的方向移动。如果我释放该键并继续按住第二次按下的键,则角色不会改变方向,直到您释放所述键并重新按下。这破坏了我的游戏感觉,我需要一些帮助来解决这个问题:) 这是我的角色控制器脚本:


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

public class playerController : MonoBehaviour
{
    public float moveSpeed = 5f;

    public Rigidbody2D rb;
    public Animator anim;
    Vector2 movement; 

    void Start()
    {
        anim = GetComponent<Animator>();
    }
    
    void Update()
    {
        
        movement.x = Input.GetAxisRaw("Horizontal"); //gets axis as vector2 
        movement.y = Input.GetAxisRaw("Vertical");

        anim.SetFloat("Horizontal", movement.x);
        anim.SetFloat("Vertical", movement.y);  //sets animation parameters
        anim.SetFloat("Speed", movement.sqrMagnitude);

        if (Input.GetAxisRaw("Horizontal") == 1 || Input.GetAxisRaw("Horizontal") == -1 || Input.GetAxisRaw("Vertical") == 1 || Input.GetAxisRaw("Vertical") == -1) 
//If statement to set the correct idle animation (idle right, left, down) based off last direction.
        {
            anim.SetFloat("LastHorizontal", Input.GetAxisRaw("Horizontal"));
            anim.SetFloat("LastVertical", Input.GetAxisRaw("Vertical"));

        }
    }  
    
    void FixedUpdate()
    {

        if (Mathf.Abs(movement.x) > Mathf.Abs(movement.y)) //if statement disables diagonal movement
        {
            movement.y = 0;
        }
        else
        {
            movement.x = 0;
        }

        rb.MovePosition(rb.position + movement.normalized * moveSpeed * Time.fixedDeltaTime); //applies movement to player
    }
}

【问题讨论】:

  • 只是一件小事来改进你的代码,你似乎在同一个函数中多次使用 Input.GetAxisRaw。考虑调用一次并将其保存到变量中,这样可以提高代码的可读性和效率。
  • anim.SetFloat("Horizontal", movement.x);anim.SetFloat("LastHorizontal", Input.GetAxisRaw("Horizontal")); 之间到底有什么区别?对于 WASD,它们将始终是完整值 -101 无论如何...
  • 在 FixedUpdate 方法中,您试图禁用对角线移动,但您的 if 语句是错误的。如果玩家试图沿对角线移动,motion.x 和 y 可能相等。这意味着您的 else 语句将被执行,从而使玩家水平移动。
  • 很久没用Unity了,有两个建议。首先,您的问题可能更适合GameDev。其次,正如@66Gramms 指出的那样,原因似乎是由于您的else 函数迫使玩家水平移动。 FixedUpdate() 多久运行一次?如果它在按键被按下时运行,即使按键由于接收到的初始值而发生变化,它也可能不会更新。按下键时,有什么办法让它执行吗?只是一些思考的食物。

标签: c# unity3d controller character


【解决方案1】:

我不会在您的固定更新中只改变一个轴上的移动,而是尝试强制每个键的方向向量。所以你的代码看起来像这样:

void FixedUpdate()
{
    Vector3 movement = Vector3.zero
    if (Input.GetKey(KeyCode.W))
    {
        movement.y = 1;
    }
    else if (Input.GetKey(KeyCode.S))
    {
        movement.y = -1;
    }
    else if (Input.GetKey(KeyCode.A))
    {
        movement.x = -1;
    }
    else if (Input.GetKey(KeyCode.D))
    {
        movement.x = 1;
    }
    else 
    {
        movement = Vector3.zero
    }
    rb.MovePosition(rb.position + movement.normalized * moveSpeed * Time.fixedDeltaTime); //applies movement to player
}

这样的结果应该是你的角色不能沿对角线移动

【讨论】:

    猜你喜欢
    • 2019-08-16
    • 1970-01-01
    • 2021-03-02
    • 1970-01-01
    • 2015-04-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多