返回

unity3d-Unity-对象不会停止移动。使用新的输入系统

发布时间:2022-06-28 01:35:58 308
# 脚本

我的播放器从不停止移动,x 上的输入始终为 -.59f,z 上的输入始终为 .59f;

input = new Vector3(move.ReadValue().x, 0, move.ReadValue().y);

输入是对从输入资产生成的脚本的引用;

我的脚本:

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

public class PlayerController : MonoBehaviour
{
    Inputs playerControlls;
    InputAction move, look;

    [SerializeField] float speed = 10;
    [SerializeField] float maxSpeed;
    [SerializeField] float rotationSpeed = 450;
    Rigidbody rb;
    Vector3 input;

    Quaternion toRoatation;

    Plane plane = new(Vector3.up, Vector3.zero);

    private void Awake()
    {
        playerControlls = new Inputs();
        rb = GetComponent();
    }


    private void Update()
    {
        input = new Vector3(move.ReadValue().x, 0, move.ReadValue().y);

        print(move.ReadValue().x);
    }

    private void FixedUpdate()
    {
        rb.velocity += input.normalized * (speed * Time.fixedDeltaTime);
        rb.velocity = Vector3.ClampMagnitude(rb.velocity, maxSpeed);

        Ray ray = Camera.main.ScreenPointToRay(Mouse.current.position.ReadValue());

        if (plane.Raycast(ray, out var enter))
        {
            var hitpoint = ray.GetPoint(enter);

            var dir = hitpoint - transform.position;

            toRoatation = Quaternion.LookRotation(dir);

            transform.rotation = Quaternion.RotateTowards(transform.rotation, toRoatation, rotationSpeed * Time.fixedDeltaTime);

        }

    }

    private void OnEnable()
    {


        move = playerControlls.Player.Move;
        look = playerControlls.Player.Look;
        move.Enable();
        look.Enable();
    }

    private void OnDisable()
    {
        move.Disable();
        look.Disable();
    }
}

我已经在这里看到了一些建议,比如写这篇文章,但它也不起作用;也许有人有主意?

playerControlls.Player.Move.started += ctx => input = ctx.ReadValue();
playerControlls.Player.Move.performed += ctx => input = ctx.ReadValue();
playerControlls.Player.Move.canceled += ctx => input = ctx.ReadValue();

WASD设置在Vector2上

特别声明:以上内容(图片及文字)均为互联网收集或者用户上传发布,本站仅提供信息存储服务!如有侵权或有涉及法律问题请联系我们。
举报
评论区(0)
按点赞数排序
用户头像