c# - 我在跳跃时遇到玩家移动问题
问题描述
我面临的唯一问题是玩家只有在他们建立动力时才会跳跃,我不希望玩家在站立或移动时通过按空间跳跃我尝试了velocity.y = jumphight这也不起作用
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovment : MonoBehaviour
{
public CharacterController controller;
public float speed = 12f;
public float gravity = -9.81f;
Vector3 velocity;
public Transform groundCheck;
public float groundDistance = 0.4f;
public LayerMask groundMask;
bool isGrounded;
public float JumpHight = 3f;
void Update()
{
isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, groundMask);
if(isGrounded && velocity.y<0)
{
controller.slopeLimit = 45.0f;
velocity.y = -2f;//when player is grounded it just put some gravity to him all the time
}
float x = Input.GetAxis("Horizontal");//moves left and right
float z = Input.GetAxis("Vertical");//moves up and down
Vector3 move = transform.right * x + transform.forward * z;
controller.Move(move*speed*Time.deltaTime);
if(Input.GetButtonDown("Jump") && controller.isGrounded)
{
velocity.y = Mathf.Sqrt(JumpHight * -2f * gravity);
}
velocity.y += gravity * Time.deltaTime;
controller.Move(velocity * Time.deltaTime);
}
}
解决方案
你可以使用 ForceMode force 在刚体上使用 addforce ,如果你不希望你的物理依赖于 fps 使用 FixedUpdate 而不是 update
GetComponent<Rigidbody>().AddForce(Vector3.up,ForceMode.Force);
推荐阅读
- python-3.x - 使用带有 aws lambda 部署包的轮子
- go - 如何在 golang 中为 new relic(Golang New relic integration)创建通用或全局上下文?
- python - tkinter,Checkbutton,相同的代码在def或单个文件中具有不同的结果
- lua - FiveM 'then' 预计在 '=' 附近
- powerbi - 如何计算动态日期中客户数量的增长百分比
- gitpython - 在集群上使用 qsub 提交作业时,使用 gitpython 获取当前哈希不起作用
- php - laravel 应用程序在浏览器上访问时显示 laravel 文件结构
- excel - VBA 受保护的工作表可防止宏替换文本,但单元格不受保护
- c# - 注册 Linq.Expression.Optimizer 以在全球范围内工作
- algorithm - 拓扑排序卡恩算法 BFS 或 DFS