Unity Jumping issue for 3d fps



你好,我正在尝试为fps游戏创建一个动作脚本。我可以移动,但跳转功能不起作用。我不确定问题是代码还是我设置角色的方式。在调试日志中,它注册了我按下的空格键,在调试中它说:FixedUpdate Jump UnityEngine.Debug:Log (object) PlayerMovement:FixedUpdate () (at 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");            
}
}
}

刚体我也在使用刚开始使用的Rigidbody3D,下面是它的设置方法。

这是字符控制器字符控制器

这是统一中的玩家运动玩家运动

那么,我做错了什么,这样我的角色就不会跳了?如果可能的话,你能解释一下吗?因为我也在努力理解它背后的概念。(如果重要的话,我使用统一2020.2.2f1。(

问题是永远不应该同时使用ChracacterController和Rigidbody。原因是它变得很乱,它做两种不同的物理计算,你可以自己用它们中的任何一种来做。

我什么时候应该使用刚体

刚体应该在2d游戏中使用(到目前为止,2d还没有内置的角色控制器(。在三维中,可以选择刚体或角色控制器。刚体应该用于飞机、船只等,也适用于任何涉及物理的玩家。对于非玩家使用,刚体应该用于任何东西,比如子弹或球。任何使用物理学的物体。

我什么时候应该使用CharacterController

角色控制器应该用于玩家,而不是像飞机或船这样严重涉及物理的物体。CharacterController具有特殊的内置功能,允许它在有特定坡度限制的情况下爬坡。它们还有台阶高度,可以控制玩家在90°面部(如楼梯(上的高度。


我听说这个动作很好,你只想让跳跃起作用。在这种情况下,我将使用CharacterController进行跳跃。

float currentGravity;
Vector3 velocity;
public float jump = 2f;
public float gravity = -9.81f;
public float groundDist = 0.5f;
public LayerMask groundMask;
void Update()
{
if (Physics.Raycast(transform.position, -transform.up, groundedDist, groundedMask)
{
currentGravity = -0.5f; //to go slowly down because the player is not truly grounded.
if (Input.GetKeyDown(KeyCode.Space))
{
velocity.y += Mathf.Sqrt(jump * -2 * gravity);
}
}
else
{
currentGravity += gravity;
}
velocity.y += currentGravity;
GetComponent<CharacterController>().Move(velocity);
}

这是我为添加跳转而编写的脚本(未经测试(。基本上,我向下光线投射(第10行(,最大距离groundDist。如果玩家离地面足够近,它会慢慢下降,刚好撞到地面(第12行(。在相同的if语句中,我检测玩家是否按下了跳跃键(第14行(,然后用Brackeys编写的物理方程将跳跃添加到玩家(第16行(。然后,如果玩家不在地面上(第19行:使用光线投射if语句中的else(,它将逐渐增加重力(第21行(。第23行:实际将重力应用于跳跃速度(如果玩家跳跃(。最后,第24行:我使用Move将角色向下移动速度。


你应该可以将其添加到你的脚本中,但如果你需要帮助,请回复,我会添加它。如果你看到/遇到任何错误,请告诉我,我会尽力修复它们。

链接:

  • 刚体还是角色控制器
  • Brackeys角色控制器教程

最新更新