我有我的跳跃脚本在统一的问题,当我按下跳跃按钮什么也没有发生。我的猜测是,它的地面检查是使这个问题。每件事都设置正确的地面有地面层等。我也尝试了跳跃的方法和脚本的工作完美,我也没有得到任何错误的统一。
我试着重新做地面检查。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
//Floats
//Speed Settings
private float movementSpeed;
public float normalSpeed = 3f;
public float sprintSpeed = 5f;
//Stamina
public float currentStamina;
public float staminaUsageMultiplier = 2.5f;
public float stamina = 100f;
//Gravity
public float gravity = -9.8f;
//Jump Settings
public float jumpHight = 3f;
//Inputs
private float horInput;
private float verInput;
//Bools
private bool isGrounded;
//Components
private CharacterController controller;
//Vectors
private Vector3 move;
private Vector3 velocity;
//Transforms
public Transform groundCheck;
//LayerMaskes
public LayerMask groundLayer;
private void Awake()
{
controller = GetComponent<CharacterController>();
}
void Start()
{
currentStamina = stamina;
}
void Update()
{
MovementMethod();
GravityMethod();
JumpMethod();
}
void MovementMethod()
{
horInput = Input.GetAxis("Horizontal");
verInput = Input.GetAxis("Vertical");
move = transform.forward * verInput + transform.right * horInput;
controller.Move(move * movementSpeed * Time.deltaTime);
if(Input.GetKey(KeyCode.LeftShift) && stamina != 0)
{
movementSpeed = sprintSpeed;
currentStamina -= staminaUsageMultiplier * Time.deltaTime;
}
else
{
movementSpeed = normalSpeed;
}
}
void GravityMethod()
{
isGrounded = Physics.CheckSphere(groundCheck.position, 0.5f, groundLayer);
if(isGrounded && velocity.y <0)
{
velocity.y = -2f;
}
velocity.y += gravity * Time.deltaTime;
controller.Move(velocity * Time.deltaTime);
}
void JumpMethod()
{
if(Input.GetKey(KeyCode.Space) && isGrounded)
{
velocity.y = Mathf.Sqrt(jumpHight * -2f * gravity);
}
}
}
2条答案
按热度按时间3phpmpom1#
检查你的groundLayer和groundCheckPosition。我猜你的groundLayer不正确或者你的groundcheck点太高了。你可以使用Debug.DrawLine使Phisic.CheckSphere可见。
u3r8eeie2#
为什么要乘以重力?速度。y = Mathf平方(跳跃高度 * -2f * 重力);我也不认为7.74的速度足以让它离开地面
7.74因为:平方和(3 * -2 * -9.8)