Hi!
I have a small problem. I managed to set the movement speed to a certain value when the character is crawling, crouching, walking or running. The character only runs if its Standing, but if i hit Play the character doesn’t run for the first time i need to crouch or prone down then stand up again to be able to run. My question is, how do i incorporate a script in this that checks if the character is already standing when i hit the Play button in Unity? It might be the easiest thing in the world but somehow nothing worked that i tried out.
Thank you in advance for your answer.
public class PlayerMove : MonoBehaviour
{
[SerializeField] private string horizontalInputName;
[SerializeField] private string verticalInputName;
[SerializeField] private float walkSpeed, runSpeed, crouchSpeed, proneSpeed;
[SerializeField] private float runBuildUpSpeed;
[SerializeField] private KeyCode runKey;
private float movementSpeed;
[SerializeField] private float slopeForce;
[SerializeField] private float slopeForceRayLength;
private CharacterController charController;
[SerializeField] private AnimationCurve jumpFallOff;
[SerializeField] private float jumpMultiplier;
[SerializeField] private KeyCode jumpKey;
[SerializeField] private KeyCode crouchKey;
[SerializeField] private KeyCode proneKey;
private bool isJumping;
private bool isCrouching;
private bool isProne;
private bool isRunning;
private bool isStanding;
private void Awake()
{
charController = GetComponent<CharacterController>();
charController.height = 2.0f;
}
private void Update()
{
PlayerMovement();
}
//MOVE
private void PlayerMovement()
{
float horizInput = Input.GetAxis(horizontalInputName);
float vertInput = Input.GetAxis(verticalInputName);
Vector3 forwardMovement = transform.forward * vertInput;
Vector3 rightMovement = transform.right * horizInput;
charController.SimpleMove(Vector3.ClampMagnitude(forwardMovement + rightMovement, 1.0f) * movementSpeed);
if ((vertInput != 0 || horizInput != 0) && OnSlope())
charController.Move(Vector3.down * charController.height / 2 * slopeForce * Time.deltaTime);
SetMovementSpeed();
JumpInput();
CrouchInput();
ProneInput();
}
private void SetMovementSpeed()
{
if (Input.GetKey(runKey) && isStanding)
{
movementSpeed = runSpeed;
}
else if (isCrouching)
movementSpeed = crouchSpeed;
else if (isProne)
movementSpeed = proneSpeed;
else
movementSpeed = walkSpeed;
}
//JUMP
private bool OnSlope()
{
if (isJumping)
return false;
RaycastHit hit;
if (Physics.Raycast(transform.position, Vector3.down, out hit, charController.height / 2 * slopeForceRayLength))
if (hit.normal != Vector3.up)
return true;
return false;
}
private void JumpInput()
{
if (Input.GetKeyDown(jumpKey) && !isJumping)
{
isJumping = true;
StartCoroutine(JumpEvent());
}
}
private IEnumerator JumpEvent()
{
charController.slopeLimit = 90.0f;
float timeInAir = 0.0f;
do
{
float jumpForce = jumpFallOff.Evaluate(timeInAir);
charController.Move(Vector3.up * jumpForce * jumpMultiplier * Time.deltaTime);
timeInAir += Time.deltaTime;
yield return null;
} while (!charController.isGrounded && charController.collisionFlags != CollisionFlags.Above);
charController.slopeLimit = 45.0f;
isJumping = false;
}
//CROUCH
public enum CharacterStance { Stand, Crouch, Prone };
private CharacterStance currentStance = CharacterStance.Stand;
private void CrouchInput()
{
if (Input.GetKeyDown(crouchKey))
{
if (currentStance != CharacterStance.Crouch)
{
currentStance = CharacterStance.Crouch;
}
else
{
currentStance = CharacterStance.Stand;
}
updateHeight();
}
}
private void ProneInput()
{
if (Input.GetKeyDown(proneKey))
{
if (currentStance != CharacterStance.Prone)
{
currentStance = CharacterStance.Prone;
}
else
{
currentStance = CharacterStance.Stand;
}
updateHeight();
}
}
private void updateHeight()
{
switch (currentStance)
{
case CharacterStance.Stand:
charController.height = 2.0f;
isCrouching = false;
isProne = false;
isStanding = true;
break;
case CharacterStance.Crouch:
charController.height = 1.5f;
isCrouching = true;
isRunning = false;
isStanding = false;
break;
case CharacterStance.Prone:
charController.height = 1.0f;
isProne = true;
isRunning = false;
isStanding = false;
break;
}
}
}