c# - 球员跳得不好
问题描述
我可以移动播放器引用此 YouTube 视频。 Unity 中的第一人称动作 - FPS 控制器
但是,当玩家跳跃时,如果行进方向是墙壁,则玩家不能很好地跳跃。 像这样
如果我从远处跳,我可以跳到墙上。
相机脚本:
public float mouseSensitivity;
[SerializeField]
Transform playerBody = default;
float xRotation = 0f;
private void Start()
{
Cursor.lockState = CursorLockMode.Locked;
}
void Update()
{
float mouseX = Input.GetAxis("Mouse X") * mouseSensitivity * Time.deltaTime;
float mouseY = Input.GetAxis("Mouse Y") * mouseSensitivity * Time.deltaTime;
xRotation -= mouseY;
xRotation = Mathf.Clamp(xRotation, -90f, 90f);
transform.localRotation = Quaternion.Euler(xRotation, 0f, 0f);
playerBody.Rotate(Vector3.up * mouseX);
}
运动脚本:
[SerializeField]
CharacterController CharacterController = default;
[SerializeField]
LayerMask groundMask = default;
[SerializeField]
Transform groundCheck = default;
float groundDistance = 0.4f;
bool isGrounded = false;
public float playerSpeed;
public float playerGravity;
public float playerJumpSpeed;
Vector3 velocity;
private 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;
CharacterController.Move(move * playerSpeed * Time.deltaTime);
if (Input.GetButtonDown("Jump") && isGrounded)
{
velocity.y = Mathf.Sqrt(playerJumpSpeed * -2f * playerGravity);
}
velocity.y += playerGravity * Time.deltaTime;
CharacterController.Move(velocity * Time.deltaTime);
}
请告诉我某人
解决方案
将 Character 控制器的 Step Offset 设置为 0 可消除振动。它不能再小步走,但现在我可以跳得很好了。
推荐阅读
- python - 根据订单、模具优化组合和打印数量
- javascript - JavaScript 等待函数
- c++ - 如何将全局范围函数声明为命名空间类的朋友?
- flash - WebSphere Management Center (CMC) 的闪存寿命终止意味着什么?
- python - 在python中创建包含特定列表值的列表列表
- wxpython - wxPython 方法 Freeze 将小部件变为黑色
- javascript - 如何通过shopify中的车把抓取产品手柄的最后一个字?
- python - 使用 beautifulSoup 和 selenium NBA 统计数据进行网络抓取
- python - 运行 python3.8 -m pip install pyAudio 时出现 pyAudio 错误。刚刚安装了 C++ 工具
- node.js - SyntaxError:不能在使用 jest 的模块外部使用 import 语句