通过脚本更改水平和垂直输入

Change Horizontal and Vertical input via script

提问人:Gryffox 提问时间:8/27/2023 更新时间:8/27/2023 访问量:34

问:

我有一个使用输入的脚本。GetAxisRaw(“Horizontal”) 设置 x,与 y 的 Vertical 相同。这些键是直接在 unity 设置中预定义的,但我希望能够通过变量更改它,以便玩家可以选择他的键。怎么做?它应该被 keyCode 替换,还是可以通过脚本直接通过水平键和垂直键进行更改?提前致谢

代码:

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

public class PlayerMovement : MonoBehaviour
{
   public float MoveSmoothTime;
   public float GravityStrenght;
   public float JumpStrenght;
   public float WalkSpeed;
   public float RunSpeed;

   private CharacterController Controller;
   private Vector3 CurrentMoveVelocity;
   private Vector3 MoveDampVelocity;

   private Vector3 CurrentForceVelocity;

    void Start()
    {
        Controller = GetComponent<CharacterController>();
    }

    void Update()
    {
        Vector3 PlayerInput = new Vector3
        {
            x = Input.GetAxisRaw("Horizontal"),
            y = 0f,
            z = Input.GetAxisRaw("Vertical")
        };

        if(PlayerInput.magnitude > 1f)
        {
            PlayerInput.Normalize();
        }

        Vector3 MoveVector = transform.TransformDirection(PlayerInput);
        float CurrentSpeed = Input.GetKey(KeyCode.LeftShift) ? RunSpeed : WalkSpeed;

        CurrentMoveVelocity = Vector3.SmoothDamp(
            CurrentMoveVelocity,
            MoveVector * CurrentSpeed,
            ref MoveDampVelocity,
            MoveSmoothTime
        );

        Controller.Move(CurrentMoveVelocity * Time.deltaTime);

        Ray groudCheckRay = new Ray(transform.position, Vector3.down);
        if(Physics.Raycast(groudCheckRay, 1.1f))
        {
            CurrentForceVelocity.y = -2f;

            if (Input.GetKey(KeyCode.Space))
            {
                CurrentForceVelocity.y = JumpStrenght;
            }
        }
        else
        {
            CurrentForceVelocity.y -= GravityStrenght * Time.deltaTime;
        }

        Controller.Move(CurrentForceVelocity * Time.deltaTime);
    }
}

我尝试用 getKey 替换,但它不起作用,因为 x 轴需要 2 个方向/输入。

C# 输入 3D

评论


答: 暂无答案