- Home /
The question is answered, right answer was accepted
Carry forward velocity against mouse movements
Two days ago, I had some help fixing being able to stop on a dime in the air and that issue was fixed. Now the only issue left is that I can still turn in the air if I hold W and look where I want to go. I would like that the character carries the way they jumped no matter any input that is put in like real life. Any help on fixing this issue is greatly appreciated as I am not sure where to start to fix that issue, any other comments are also welcome and appreciated.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
public CharacterController controller;
public Transform groundCheck;
public LayerMask groundMask;
CharacterController playerCol;
public float crouchSpeed = 2.5f;
public float walkSpeed = 5f;
public float runSpeed = 7f;
public float jumpHeight = 0.7f;
public float crouchHeight = 1.1f;
public float gravity = -22f;
public float groundDistance = 0.4f;
float standRaycastRange = 1.55f;
private float baseSpeed = 0f;
float originalHeight;
float x = 0f;
float z = 0f;
static bool canSprint = true;
static bool crouched = false;
static bool canJump = true;
bool isGrounded;
Vector3 velocity;
static bool isSprinting = false;
void Start()
{
playerCol = GetComponent<CharacterController>();
originalHeight = playerCol.height;
baseSpeed = walkSpeed;
}
void Update()
{
//Basic movement-------------------------------------------------------------------
isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, groundMask);
if (isGrounded && velocity.y < 0)
{
x = Input.GetAxis("Horizontal");
z = Input.GetAxis("Vertical");
if (velocity.y < 0)
{
velocity.y = -2f;
}
}
Vector3 move = transform.right * x + transform.forward * z;
controller.Move(move * baseSpeed * Time.deltaTime);
//Jump--------------------------------------------------
if (Input.GetButtonDown("Jump") && isGrounded && canJump)
{
velocity.y = Mathf.Sqrt(jumpHeight * -2f * gravity);
}
velocity.y += gravity * Time.deltaTime;
controller.Move(velocity * Time.deltaTime);
if (Input.GetButtonDown("Jump") && isGrounded && crouched)
{
if (!Physics.Raycast(transform.position, Vector3.up, standRaycastRange))
{
UnCrouch();
}
}
//Sprint--------------------------------------------------------------
if (Input.GetKey(KeyCode.LeftShift) && (Input.GetKey(KeyCode.W)) && isGrounded && canSprint)
{
Sprint();
}
if (Input.GetKeyUp(KeyCode.W) && isSprinting)
{
UnSprint();
}
else if (Input.GetKeyUp(KeyCode.LeftShift) && isGrounded && canSprint)
{
UnSprint();
}
if (Input.GetKey(KeyCode.S) && canSprint)
{
UnSprint();
}
if (!Input.GetKey(KeyCode.LeftShift) && isGrounded)
{
UnSprint();
}
if (Input.GetKeyDown(KeyCode.LeftShift) && isGrounded && crouched)
{
if (!Physics.Raycast(transform.position, Vector3.up, standRaycastRange))
{
UnCrouch();
Sprint();
}
}
//Crouch----------------------------------------------------------------
if (Input.GetKeyDown(KeyCode.C) && isGrounded && !isSprinting)
{
crouched = !crouched;
if (crouched) Crouch();
else if (!Physics.Raycast(transform.position, Vector3.up, standRaycastRange) && !crouched && isGrounded)
{
UnCrouch();
}
}
//Sprint-------------------
void Sprint()
{
baseSpeed = runSpeed;
crouched = false;
isSprinting = true;
}
void UnSprint()
{
baseSpeed = walkSpeed;
isSprinting = false;
}
//Crouch-------------------------------
void Crouch()
{
playerCol.height = crouchHeight;
baseSpeed = crouchSpeed;
canJump = false;
canSprint = false;
crouched = true;
isSprinting = false;
}
void UnCrouch()
{
playerCol.height = originalHeight;
baseSpeed = walkSpeed;
canJump = true;
canSprint = true;
crouched = false;
}
}
}
Answer by VoidPhoenix96 · Feb 26, 2021 at 11:48 PM
When the player uses movement input, check if they are in the air. If so, do nothing. You can use Update() and a Boolean to do this.
I currently have a ground check boolean already implemented but I'm not sure how to go about checking for input in the air or how I would make that do nothing with my current code.
Use that boolean to check if you are in the air before applying movement.
if(isTouchingGround) { movementcode; }