c# - 如何在 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 接口进行分类。我该怎么做?
解决方案
我建议您使用集成的 Unity FirstPersonController
,这样您就不会重新发明轮子。但是,如果您想创建一个必须使用的原始控制器Input.GetKeyDown(yourKey)
,这将解决问题。
推荐阅读
- javascript - 在 vue js 中读取和渲染基于文本的列表
- android - 我无法使用数据绑定在属性值上设置 int
- python - 为什么我的递归没有返回所需的输出?
- html - 我可以在 django 中的 CSS 文件上加载静态文件夹吗
- javascript - FocusEvent 的错误或标准行为?
- html - 如何在 Shopify 用户注册表单中添加错误验证?
- java - 如何通过请求发送不记名令牌
- android - “解析名称服务器时出错”MongoDb 和 Xamarin.Forms Android
- maximo - Maximo 查看来自发布通道 json 请求的响应
- nginx - 如何在 NGINX 代理后面访问 FastAPI SwaggerUI 文档?