C# 三维fps的统一跳跃问题
您好,我正在尝试创建一个fps游戏的运动脚本。我的动作正常,但跳转功能不正常。我不确定问题是代码还是我设置角色的方式。在调试日志中,我按下了空格键,在调试中显示:C# 三维fps的统一跳跃问题,c#,visual-studio,unity3d,C#,Visual Studio,Unity3d,您好,我正在尝试创建一个fps游戏的运动脚本。我的动作正常,但跳转功能不正常。我不确定问题是代码还是我设置角色的方式。在调试日志中,我按下了空格键,在调试中显示:FixedUpdate Jump UnityEngine.debug:log(object)PlayerMovement:FixedUpdate()(位于Assets/PlayerMovement.cs:94) 代码如下: using System.Collections; using System.Collections.Generi
FixedUpdate Jump UnityEngine.debug:log(object)PlayerMovement:FixedUpdate()(位于Assets/PlayerMovement.cs:94)
代码如下:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[RequireComponent(typeof(Rigidbody))]
public class PlayerMovement : MonoBehaviour
{
private float _speed = 7f;
private CharacterController _charController;
private float _mouseSensitivity = 450;
private Camera _camera;
private float xRotation = 0f;
private float _minCameraview = -70f, _maxCameraview = 90f;
private Vector3 _PlayerVelocity;
public Vector3 jump;
public float jumpForce = 2.0f;
bool isJumpPressed = false;
public bool isGrounded = true;
Rigidbody rb;
// Start is called before the first frame update
void Start()
{
_charController = GetComponent<CharacterController>();
_camera = Camera.main;
Cursor.lockState = CursorLockMode.Locked;
rb = GetComponent<Rigidbody>();
jump = new Vector3(0.0f, 2.0f, 0.0f);
//if(_charController == null)
//Debug.log("No Character Controller attached to player");
}
// Update is called once per frame
void Update()
{
//Get WASD input for player
float horizontal = Input.GetAxis("Horizontal");
float vertical = Input.GetAxis("Vertical");
// Move player based on WASD input
Vector3 movement = transform.forward * vertical + transform.right * horizontal;
_charController.Move(movement * Time.deltaTime * _speed);
//Get mouse position input
float mouseX = Input.GetAxis("Mouse X") * _mouseSensitivity * Time.deltaTime;
float mouseY = Input.GetAxis("Mouse Y") * _mouseSensitivity * Time.deltaTime;
// rotate camera based off Y input from Mouse
xRotation -= mouseY;
// Clamp Camera rotation
xRotation = Mathf.Clamp(xRotation, _minCameraview, _maxCameraview);
_camera.transform.localRotation = Quaternion.Euler(xRotation, 0, 0);
// rotate player based off X input from Mouse
transform.Rotate(Vector3.up * mouseX * 3);
//Jumping
if(Input.GetKeyDown(KeyCode.Space) && isGrounded){
rb.AddForce(jump * jumpForce, ForceMode.Impulse);
isGrounded = false;
}
isJumpPressed = Input.GetKeyDown(KeyCode.Space);
if (isJumpPressed)
{
Debug.Log("Update Jump");
}
}
void FixedUpdate()
{
// dectect if player is grounded
if (_charController.isGrounded)
{
_PlayerVelocity.y = 0f;
}
// apply gravity to player
else
{
_PlayerVelocity.y += -9.18f * Time.deltaTime;
_charController.Move(_PlayerVelocity * Time.deltaTime);
}
if (isJumpPressed)
{
Debug.Log("FixedUpdate Jump");
}
}
}
使用系统集合;
使用System.Collections.Generic;
使用UnityEngine;
[RequiredComponent(类型)(刚体))]
公共类玩家运动:单一行为
{
专用浮子_速度=7f;
专用字符控制器_charController;
专用浮点鼠标灵敏度=450;
私人摄像机;
专用浮点数xRotation=0f;
私有浮动_minCameraview=-70f,_maxCameraview=90f;
私人矢量3_游戏城;
公共矢量3跳跃;
公共浮子跳线力=2.0f;
bool isJumpPressed=false;
公共bool isGrounded=true;
刚体rb;
//在第一帧更新之前调用Start
void Start()
{
_charController=GetComponent();
_camera=camera.main;
Cursor.lockState=CursorLockMode.Locked;
rb=GetComponent();
跳跃=新矢量3(0.0f、2.0f、0.0f);
//如果(_charController==null)
//log(“没有字符控制器连接到播放器”);
}
//每帧调用一次更新
无效更新()
{
//获取播放器的WASD输入
float horizontal=Input.GetAxis(“水平”);
float vertical=Input.GetAxis(“垂直”);
//基于WASD输入移动播放器
Vector3移动=transform.forward*垂直+transform.right*水平;
_charController.Move(移动*时间增量*速度);
//获取鼠标位置输入
float mouseX=Input.GetAxis(“Mouse X”)*\u mouseSensitivity*Time.deltaTime;
float mouseY=Input.GetAxis(“鼠标Y”)*\u mouseSensitivity*Time.deltaTime;
//基于鼠标的Y输入旋转相机
xRotation-=鼠标;
//夹紧摄像机旋转
xRotation=Mathf.夹具(xRotation,_minCameraview,_maxCameraview);
_camera.transform.localRotation=Quaternion.Euler(xRotation,0,0);
//根据鼠标的X输入旋转播放器
变换.旋转(Vector3.up*mouseX*3);
//跳跃
if(Input.GetKeyDown(KeyCode.Space)和&isground){
rb.附加力(跳跃*跳跃力,力模式.脉冲);
isfounded=false;
}
isJumpPressed=Input.GetKeyDown(KeyCode.Space);
如果(按下按钮)
{
Log(“更新跳转”);
}
}
void FixedUpdate()
{
//检测球员是否接地
如果(_charController.isGrounded)
{
_PlayerVelocity.y=0f;
}
//对玩家施加重力
其他的
{
_PlayerVelocity.y+=-9.18f*Time.deltaTime;
_charController.Move(_PlayerVelocity*Time.deltaTime);
}
如果(按下按钮)
{
Log(“FixeUpdate跳转”);
}
}
}
我还使用了Rigidbody3D,这也是我所不熟悉的,所以这里是如何设置它的
这是角色控制器
这是统一中的玩家运动
那么我做错了什么,这样我的角色就不会跳了?如果可能的话,你能解释一下,因为我也在努力理解它背后的概念。(如果需要的话,我使用的是unity 2020.2.2f1。)问题是,您永远不应该同时使用ChracacterController和Rigidbody。这样做的原因是它变得混乱,它进行两种不同的物理计算,你可以自己用它们中的任何一种进行计算 我什么时候应该使用刚体? 刚体应该在2d游戏中使用(到目前为止,2d游戏中还没有内置的角色控制器)。在3D中,您可以选择刚体或CharacterController。刚体应该用于飞机、船只等以及任何涉及物理的玩家。对于非玩家使用,刚体应该用于任何东西,如子弹或球。任何使用物理学的物体 我什么时候应该使用CharacterController? 角色控制器应该用于玩家,而不是像飞机或船只这样涉及大量物理的物体。CharacterController具有特殊的内置功能,允许它爬上具有特定坡度限制的斜坡。他们也有台阶高度,可以控制玩家在90°面(如楼梯)上的高度
我听说这个动作做得很好,你只想跳起来就行了。在本例中,我将使用CharacterController进行跳跃
float-current-gravity;
矢量3速度;
公共浮动跳跃=2f;
公共浮子重力=-9.81f;
公众浮标地面距离=0.5f;
公共层掩码地面掩码;
无效更新()
{
if(Physics.Raycast(transform.position,-transform.up,groundedDist,groundedMask)
{
currentGravity=-0.5f;//缓慢下降,因为玩家并没有真正落地。
if(Input.GetKeyDown(KeyCode.Space))
{
速度y+=数学Sqrt(跳跃*-2*重力);
}
}
其他的
{
当前重力+=重力;
}
速度y+=当前重力;
GetComponent().Move(速度);
}
这是我添加跳跃的脚本(未测试)。基本上,我向下光线投射(第10行),最大距离为groundDist。如果玩家离地面足够近,它会慢慢向下移动,只是碰到地面(第12行)。在相同的If语句中,我检测玩家是否按下跳跃键(第14行),然后我向玩家添加跳跃(第16行)使用由编写的物理方程。然后,如果玩家不在地面上(第19行:使用光线投射if语句中的else),它将逐渐增加重力(第21行)。第23行:实际应用