简体   繁体   English

无法使对角线和垂直/水平移动速度相同?

[英]Failing to make diagonal and vertical/horizontal movement speeds the same?

I'm relatively new to coding, and I've run into a small problem with my code.我对编码比较陌生,我的代码遇到了一个小问题。 I'm using Vector3 in unity to make my character move, but when I move diagonally, the character moves faster than vertically/horizontally.我正在统一使用 Vector3 让我的角色移动,但是当我沿对角线移动时,角色的移动速度比垂直/水平移动快。 I used a boolean fMove to account for w and s presses, and then I used another one rMove for a and d presses.我使用 boolean fMove 来控制 w 和 s 压力机,然后我使用另一个 rMove 来控制 a 和 d 压力机。 I tried to make it change the speed from 10 to 7.07 when both were true, and then return it from 7.07 to 10 when either or both is false.我试图让它在两者都为真时将速度从 10 更改为 7.07,然后在其中一个或两者为假时将其从 7.07 返回到 10。

My issue is that it only work once every 10 or so tries, and it only works for 1 second.我的问题是它每 10 次左右尝试只能工作一次,而且只能工作 1 秒。 I'm trying to adjust the priorities to get it to check the speed changing checks first, but I'm unsure if that's the correct idea.我正在尝试调整优先级以使其首先检查速度变化检查,但我不确定这是否是正确的想法。 Any help would be much appreciated.任何帮助将非常感激。

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

public class Player_Movement : MonoBehaviour
{
    //Defining Variables
    public float speed = 10.0f;
    public float jumpHeight = 4.0f;
    private float rightInput;
    private float forwardInput;
    private float jumpInput;
    private bool rMove;
    private bool fMove;

    void Start()
    {
        rMove = false;
        fMove = false;
    }

    void Update()
    {
        //Retrieve Player Input
        rightInput = Input.GetAxis("Horizontal");
        forwardInput = Input.GetAxis("Vertical");
        jumpInput = Input.GetAxis("Jump");
        
        //Player Movement
        transform.Translate(Vector3.forward * speed * Time.deltaTime * forwardInput);
        transform.Translate(Vector3.right * speed * Time.deltaTime * rightInput);
        transform.Translate(Vector3.up * jumpHeight * Time.deltaTime * jumpInput);

        //Set booleans for movement speed correction
        if(Input.GetKeyDown("w") || Input.GetKeyDown("s"))
        {
            fMove = true;
        }
        else if(Input.GetKeyDown("w") == false || Input.GetKeyDown("s") == false)
        {
            fMove = false;
        }

        if(Input.GetKeyDown("a") || Input.GetKeyDown("d"))
        {
            rMove = true;
        }
        else if(Input.GetKeyDown("a") == false || Input.GetKeyDown("d") == false)
        {
            rMove = false;
        }

        //Adjust speed for diagonal movement
        if(fMove == true && rMove ==true)
        {
            speed = 7.07f;
            speed = 2.0f;
            Debug.Log("move = " + speed);
        }
        else if(fMove == false || rMove == false)
        {
            speed = 10.0f;
            Debug.Log("move = " + speed);
        }
    }
}

For your movement, instead of using keys , you should use Axis (and buttons).对于您的运动,您应该使用 Axis (和按钮)而不是使用keys Unity has a whole Input Manager system that allows you to bind keyboard and controllers to the action (like Fire1, Jump, Horizontal, Vertical, etc.). Unity 有一个完整的输入管理器系统,允许您将键盘和控制器绑定到动作(如 Fire1、Jump、Horizontal、Vertical 等)。

A good example for a basic movement would be this:基本运动的一个很好的例子是:

[SerializeField] private Rigidbody rb;
[SerializeField] private float speed;
private float hAxis = 0;
private float vAxis = 0;

void Update() // All your inputs fetchers should stay in the Update method
{
    hAxis = Input.GetAxisRaw("Horizontal"); 
    vAxis = Input.GetAxisRaw("Vertical");
}

void FixedUpdate() // While your physics should stay in FixedUpdate method
{
    Vector3 direction = Vector3.Normalize(new Vector3(hAxis, 0, vAxis)); // Converts keyboard and joystick to an uniform vector (same magnitude)
    Vector3 newVelocity = rb.transform.TransformDirection(direction) * speed * Time.fixedDeltaTime;
    rb.velocity = new Vector3(newVelocity.x, rb.velocity.y, newVelocity.z); // We copy y velocity to avoid the gliding common issue
}

Hopefully this helps!希望这有帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM