简体   繁体   English

我已经遵循了关于为统一的fps游戏制作控件的教程。 控制工作但如果我离开控件然后我继续向左移动

[英]I have followed a tutorial on making controls for a fps game on unity. the controls work but if I leave the controls then I keep moving to the left

enter image description here I have made controls for my character to move along the X axis. 在这里输入图像描述我已经控制我的角色沿X轴移动。 They seem to be working fine but my character keeps moving left and I'm not sure why or what I have missed.I have posted code from the two separate scripts 他们似乎工作正常,但我的角色继续向左移动,我不知道为什么或我错过了什么。我已经发布了两个单独脚本的代码

I have rechecked his code several times.I have checked to see if my arrow keys, numpad keys and WASD are sticking or any other. 我已经多次重新检查他的代码。我已经检查过我的箭头键,小键盘键和WASD是否有粘连或其他任何东西。

using UnityEngine;

[RequireComponent(typeof(Rigidbody))]
public class PlayerMotor : MonoBehaviour
{
    private Vector3 velocity = Vector3.zero;

    private Rigidbody rb;

    void Start()
    {
        rb = GetComponent<Rigidbody>();
    }

    //gets a movement vector
    public void Move (Vector3 _velocity)
    {
        velocity = _velocity;
    }

    //run every physics iteration
    void FixedUpdate()
    {
        PerformMovement();
    }

    //perform movement based on velocity variable
    void PerformMovement ()
    {
        if (velocity != Vector3.zero)
        {
            rb.MovePosition(rb.position + velocity * Time.fixedDeltaTime);
        }

    }

}

And the controller: 和控制器:

using UnityEngine;

[RequireComponent(typeof(PlayerMotor))]
public class PlayerController : MonoBehaviour
{
    [SerializeField]                //makes speed show up in inspector even if set to private
    private float speed = 5f;


    private PlayerMotor motor;
    void Start ()
    {
        motor = GetComponent<PlayerMotor>();
    }

    void Update()
    {
        //Calculate movement velocity as a 3D vector
        float _xMov = Input.GetAxisRaw("Horizontal");
        float _zMov = Input.GetAxisRaw("Vertical");

        Vector3 _movHorizontal = transform.right * _xMov;
        Vector3 _movVertical = transform.forward * _zMov;


        //final movement vector
        Vector3 _velocity = (_movHorizontal + _movVertical).normalized * speed;

        //apply movement
        motor.Move(_velocity);

    }
}

I expect 0 output when not pressing buttons but it seems to make me move to the left at speed of 5 不按按钮时我期望0输出,但它似乎让我以5的速度向左移动

You have a controller connected and reporting a slight movement in one direction. 您已连接控制器并报告一个方向的轻微移动。 Use Input.GetAxis instead of Input.GetAxisRaw to let Unity handle deadzone detection. 使用Input.GetAxis而不是Input.GetAxisRaw让Unity处理死区检测。 This way, nearly neutral inputs will be treated as neutral inputs. 这样,几乎中性的输入将被视为中性输入。

void Update()
{
    //Calculate movement velocity as a 3D vector
    float _xMov = Input.GetAxis("Horizontal");
    float _zMov = Input.GetAxis("Vertical");

    Vector3 _movHorizontal = transform.right * _xMov;
    Vector3 _movVertical = transform.forward * _zMov;


    //final movement vector
    Vector3 _velocity = (_movHorizontal + _movVertical).normalized * speed;

    //apply movement
    motor.Move(_velocity);

}

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

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