首页 > 解决方案 > 如何在 c# 脚本中对 UnityEnine.Input 进行分类

问题描述

我一直在尝试为学校编写一个 3D c# 游戏,我有这个脚本:

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

public class Player : MonoBehaviour
{

//Variables
public float movementSpeed;





//Functions

public float speed = 5;
public float gravity = -5;

float velocityY = 0;

CharacterController controller;

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

public void Update()
{
    velocityY += gravity * Time.deltaTime;

    Vector3 input = new Vector3(Input.GetAxisRaw("Horizontal"), 0 Input.GetAxisRaw("Vertical"));
    input = input.normalized;

    Vector3 temp = Vector3.zero;
    if (input.z == 1)
    {
        temp += transform.forward;
    }
    else if (input.z == -1)
    {
        temp += transform.forward * -1;
    }

    if (input.x == 1)
    {
        temp += transform.right;
    }
    else if (input.x == -1)
    {
        temp += transform.right * -1;
    }

    Vector3 velocity = temp * speed;
    velocity.y = velocityY;

    controller.Move(velocity * Time.deltaTime);

    if (controller.isGrounded)
    {
        velocityY = 0;
    }
}
}

所以它是一个用 WASD 移动角色的脚本,它有重力,但 Unity 说我需要在输入系统中对 UnityEngine.Input 接口进行分类。我该怎么做?

标签: c#visual-studio

解决方案


我建议您使用集成的 Unity FirstPersonController,这样您就不会重新发明轮子。但是,如果您想创建一个必须使用的原始控制器Input.GetKeyDown(yourKey),这将解决问题。


推荐阅读