- Home /
How do you jump and keep moving to the direction your player/character was when they were on the ground, using a character controller?
I'm gonna try to keep it short. I want to figure out how to make the character/player be able to move in the direction they were once going in while jumping. The reason why is because I don't want the player to let go of the w key and just stop abruptly mid-air, it will look weird, especially if the character is continuing to fall at the same velocity as it was before the key was let go. Also, the other reason is that I'm gonna try to put in a wall jump to my character, and the video that's I'm most likely gonna use to find out how to do that made a point that the character shouldn't be able to turn immediately and "hump the wall".
here's the code for the character:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ThirdPersonMovement : MonoBehaviour
{
public CharacterController controller;
public Transform cam;
private Vector3 moveVector;
private Vector3 lastMove;
private float verticalVelocity;
public float speed = 6f;
public float gravity = -9.81f;
public float jumpHeight = -3f;
public bool _canDoubleJump = false;
private float airSpeed = 4.0f;
private float airFriction = 0.65f;
public Transform groundCheck;
public float groundDistance = 0.4f;
public LayerMask groundMask;
Vector3 velocity;
bool isGrounded = false;
public float turnSmoothTime = 0.1f;
float turnSmoothVelocity;
public float dJumpHeight = 3.5f;
private void Update()
{
isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, groundMask);
if(isGrounded && velocity.y < 0)
{
velocity.y = -2.0f;
}
float horizontal = Input.GetAxisRaw("Horizontal");
float vertical = Input.GetAxisRaw("Vertical");
Vector3 direction = new Vector3(horizontal, 0f, vertical).normalized;
if (direction.magnitude >= 0.1f)
{
float targetAngle = Mathf.Atan2(direction.x, direction.z) * Mathf.Rad2Deg + cam.eulerAngles.y;
float angle = Mathf.SmoothDampAngle(transform.eulerAngles.y, targetAngle, ref turnSmoothVelocity, turnSmoothTime);
transform.rotation = Quaternion.Euler(0f, angle, 0f);
Vector3 moveDir = Quaternion.Euler(0f, targetAngle, 0f) * Vector3.forward;
controller.Move(moveDir.normalized * speed * Time.deltaTime);
}
if (controller.isGrounded == true)
{
_canDoubleJump = false;
turnSmoothTime = 0f;
controller.slopeLimit = 45.0f;
if (Input.GetButtonDown("Jump"))
{
velocity.y = Mathf.Sqrt(jumpHeight * gravity * (-2.0f));
_canDoubleJump = true;
turnSmoothTime = 0.8f;
controller.slopeLimit = 90.0f;
}
}
else
{
if (Input.GetButtonDown("Jump"))
{
if (_canDoubleJump == true)
{
velocity.y = Mathf.Sqrt(dJumpHeight * gravity * (-2.0f));
_canDoubleJump = false;
turnSmoothTime = 0.8f;
moveVector = lastMove;
if (_canDoubleJump == false)
{
turnSmoothTime = 0.8f;
moveVector = lastMove;
}
}
}
}
//if (Input.GetButtonDown("Jump"))
//{
//if (controller.isGrounded == false)
//{
//velocity.y = Mathf.Sqrt(jumpHeight * gravity * (-2.0f));
//}
//else
//{
//}
//}
//if (Input.GetButtonDown("Jump"))
//if (controller.isGrounded)
velocity.y += gravity * Time.deltaTime;
controller.Move(velocity * Time.deltaTime);
if (Input.GetKey("left shift"))
{
speed = 16f;
}
else
{
speed = 8f;
}
moveVector.y = 0;
moveVector.Normalize();
moveVector *= speed;
moveVector.y = verticalVelocity;
controller.Move(moveVector * Time.deltaTime);
lastMove = moveVector;
}
}
Here's a vid of it happening if you don't understand what I mean: https://www.youtube.com/watch?v=_vkYQHkqbKU
P.S. sorry if this sounds demanding in some way. I've just been trying to figure out how to solve this problem for the past few hours and I just really want to learn how to do it and move on.
P.S.S. I'm a visual learner so please don't try to explain it to me with only words.
Your answer
Follow this Question
Related Questions
Dampening Movement while in the Air 0 Answers
How to make your character jump? 1 Answer
Character floats down after jumping 0 Answers
How to prevent player from moving in air while jumping in place ?? 0 Answers
Movement Jumping Help 0 Answers