Прыжок во время спринта
Вообщем-то проблема в том что при прыжке и спринте одновременно скорость передвижения не изменеятся( Код в придачу ).
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[RequireComponent(typeof(CharacterController))]
public class PlayerMovement : MonoBehaviour {
public float walkSpeed = 3.0f;
public float sprintSpeed = 30.0f;
public float jumpForce = 10.0f;
public float fallSpeed = 30.0f;
[HideInInspector] public float deltaX = 0.0f;
[HideInInspector] public float deltaY = 0.0f;
[HideInInspector] public float deltaZ = 0.0f;
private float _currentSpeed;
private bool _isGrounded = false;
private CharacterController _character = null;
private void Awake()
{
Initialize();
Cursorlock();
}
private void Update()
{
OnCheckGrounded();
OnMove();
OnJump();
OnBakeMovement();
}
private void Initialize()
{
_character = GetComponent<CharacterController>();
}
private void OnCheckGrounded()
{
Ray ray = new Ray(transform.position, -transform.up);
RaycastHit hit;
float distanceToHit = 1.1f;
if (Physics.Raycast(ray, out hit, distanceToHit))
{
_isGrounded = true;
}
else
{
_isGrounded = false;
}
}
private void OnMove()
{
_currentSpeed = walkSpeed;
if (Input.GetKey(KeyCode.LeftShift))
{
_currentSpeed = sprintSpeed;
}
if(Input.GetButtonDown("Jump") && Input.GetKey(KeyCode.LeftShift))
{
_currentSpeed = sprintSpeed / 100;
}
deltaX = Input.GetAxis("Horizontal") * _currentSpeed;
deltaZ = Input.GetAxis("Vertical") * _currentSpeed;
}
private void OnJump()
{
if (_isGrounded == true)
{
deltaY = 0.0f;
if (Input.GetButtonDown("Jump"))
{
deltaY = jumpForce;
}
}
else
{
deltaY -= fallSpeed * Time.deltaTime;
}
}
private void OnBakeMovement()
{
Vector3 movement = new Vector3(deltaX, 0.0f, deltaZ);
movement = transform.TransformVector(movement);
movement = Vector3.ClampMagnitude(movement, _currentSpeed);
movement.y = deltaY;
movement *= Time.deltaTime;
_character.Move(movement);
}
private void Cursorlock()
{
Cursor.lockState = CursorLockMode.Locked;
if (Input.GetKeyDown(KeyCode.Escape))
{
Cursor.lockState = CursorLockMode.None;
}
}
}
Ответы (2 шт):
Автор решения: Alemkhan Utepkaliev
→ Ссылка
Вы используйте проверку нажатия где доминирует первое условие LeftShift нажатие, потому что она нажата в двух случаях, и в большинстве случаев всегда будет именно она работать, и лишь на короткое время пока была нажата Jump и LeftShift придерживается выполняется второе условие. И снова переходиться к первому условиему.
Предлагаю делать проверку что при зажатой LeftShift Вы находится на земле, или другой удобный способ проверки.
if (Input.GetKey(KeyCode.LeftShift) && OnCheckGrounded()) {
_currentSpeed = sprintSpeed;
}
if (Input.GetButtonDown("Jump") && Input.GetKey(KeyCode.LeftShift)) {
_currentSpeed = sprintSpeed / 100;
}
Автор решения: YellowDollar
→ Ссылка
Исправилось таким образом
if (Input.GetKey(KeyCode.LeftShift) && _isGrounded == true)
{
_currentSpeed = sprintSpeed;
}
if(Input.GetKey(KeyCode.LeftShift) && _isGrounded == false)
{
_currentSpeed = 10.0f;
}