c# - unity 3D 角色 c# 脚本,跳转不起作用?
问题描述
所以我有这个第一人称玩家控制器 c# 脚本,(我遵循了 brackeys 的教程)并且我有跳跃输入编码,基本上是逐字逐句,但由于某种原因它不会跳跃,而是一旦我开始游戏,玩家只是无限漂浮起来,这绝对不是我想要的。这是脚本,以防你们中的任何人都知道如何修复它(抱歉,如果错误是拼写错误或其他什么,但这将有很大帮助):
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
public CharacterController controller;
public float speed = 12f;
public float gravity = -9.81f;
public LayerMask groundMask;
public float groundDistance = 0.4f;
public Transform groundCheck;
public float jumpHeight = 3f;
Vector3 velocity;
bool isGrounded;
void Update()
{
isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, groundMask);
if(isGrounded && velocity.y < 0)
{
velocity.y = -2f;
}
float x = Input.GetAxis("Horizontal");
float z = Input.GetAxis("Vertical");
Vector3 move = transform.right * x + transform.forward * z;
controller.Move(move * speed * Time.deltaTime);
if(Input.GetButtonDown("Jump") && isGrounded)
{
velocity.y = Mathf.Sqrt(jumpHeight * -2f * gravity);
}
velocity.y += gravity * Time.deltaTime;
controller.Move(velocity * Time.deltaTime);
}
}
提前致谢。
解决方案
推荐阅读
- javascript - 在@Html.DropDownList 中从控制器端绑定下拉列表选择的值
- html - 有没有办法在文本上放置绝对定位?
- azure-devops - 如何将 PowerShell 命令的输出存储在 Azure DevOps 管道输出变量中并在下一阶段使用?
- c - 用于写入 /proc 文件的内核 API
- ios - iOS,相同尺寸的UIView在不同的iphone上显示不同的尺寸?
- webassembly - 为什么 WebAssembly 是安全的,什么是线性内存模型
- javascript - jQuery Append in the Loop 打印未定义
- python - Python - 按 ID 选择具有最大值的最小日期
- javascript - 反应按钮 onClick 不打印到控制台
- html - 如何从 ckeditor 以相同的样式呈现 HTML