How to get jumping to return to 0,0,0 quicker
I'll start off by saying this is my first week into trying to design a game through unity. I maybe have about 20 hours in unity at the moment, and have been just watching YouTube videos to create a basic character controller.
By piecing together the mechanics I learned, as well as adding some lines of my own; I was able to get a controller that had decent movement. My character moves, and looks with perfect precision and gravity, and the jump mechanic implemented jumps with perfect physics and gravity. However I've noticed upon landing I am not able to re-jump immediately. Upon investigating and changing certain stuff to the script, I realized that upon jumping, and then falling I would get to 1.27 (or something) in the speed I want then take approximately 1 second to settle back down to be able to clear the timer and jump again.
Essentially I'm at a loss trying to figure out how to get that last .02 to go a lot faster and just get to the ground. I've tried a bunch of various things, but I don't see anyone with this particular jumping issue so I would appreciate any help or guidance someone can bring. Here is the current movement script I am implementing....
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class PlayerController : MonoBehaviour { // Run and Crouch Serializations
public bool isSprinting = false;
[SerializeField] public float sprintingMultiplier;
public bool isCrouching = false;
[SerializeField] public float crouchingMultiplier;
public bool isJumping = false;
// POV and Player Movement Serializations
[SerializeField] Transform playerCamera = null;
[SerializeField] float mouseSensitivity = 3.5f;
[SerializeField] float walkSpeed = 6.0f;
[SerializeField] public float playerSpeed;
[SerializeField] float gravity = -9.81f;
[SerializeField] [Range(0.0f, 0.5f)] float moveSmoothTime = 0.3f;
[SerializeField] [Range(0.0f, 0.5f)] float mouseSmoothTime = 0.03f;
[SerializeField] bool lockCursor = true;
float cameraPitch = 0.0f;
float velocityY = 0.0f;
CharacterController controller;
Vector2 currentDir = Vector2.zero;
Vector2 currentDirVelocity = Vector2.zero;
Vector2 currentMouseDelta = Vector2.zero;
Vector2 currentMouseDeltaVelocity = Vector2.zero;
// Jump Mechs
private float groundedTimer;
private float verticalVelocity;
[SerializeField] private float jumpHeight;
[SerializeField] private float jumpSpeed;
[SerializeField] private float jumpForce;
//More Hairy Shit
private Vector3 moveDirection = Vector3.zero;
private float directionY;
// Start is called before the first frame update
void Start()
{
controller = GetComponent<CharacterController>();
if (lockCursor)
{
Cursor.lockState = CursorLockMode.Locked;
Cursor.visible = false;
}
}
// Update is called once per frame
void Update()
{
UpdateMouseLook();
UpdateMovement();
}
void UpdateMouseLook()
{
Vector2 targetmouseDelta = new Vector2(Input.GetAxis("Mouse X"), Input.GetAxis("Mouse Y"));
currentMouseDelta = Vector2.SmoothDamp(currentMouseDelta, targetmouseDelta, ref currentMouseDeltaVelocity, mouseSmoothTime);
cameraPitch -= currentMouseDelta.y * mouseSensitivity;
cameraPitch = Mathf.Clamp(cameraPitch, -90f, 80f);
playerCamera.localEulerAngles = Vector3.right * cameraPitch;
transform.Rotate(Vector3.up * currentMouseDelta.x * mouseSensitivity);
}
void UpdateMovement()
{
//TestMovement
bool isGrounded = controller.isGrounded;
// Movement
Vector2 targetDir = new Vector2(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical"));
targetDir.Normalize();
currentDir = Vector2.SmoothDamp(currentDir, targetDir, ref currentDirVelocity, moveSmoothTime);
if (isGrounded)
velocityY = 0.0f;
velocityY += gravity * Time.deltaTime;
Vector3 velocity = (transform.forward * currentDir.y + transform.right * currentDir.x) * playerSpeed + Vector3.up * velocityY;
controller.Move(velocity * Time.deltaTime);
velocityY += gravity * Time.deltaTime;
//Hairy Shit
bool ccGrounded = controller.isGrounded;
if (isGrounded)
{
groundedTimer = 0.2f;
}
if (groundedTimer > 0)
{
groundedTimer -= Time.deltaTime;
}
if (isGrounded && verticalVelocity < 0)
{
verticalVelocity = 0f;
}
verticalVelocity += gravity * Time.deltaTime;
//Running Jumping and Crouching Values
if (Input.GetKey(KeyCode.LeftShift) && controller.isGrounded && !Input.GetKey(KeyCode.LeftControl))
{
isSprinting = true;
playerSpeed = walkSpeed * sprintingMultiplier;
}
else
{
isSprinting = false;
}
if (Input.GetKey(KeyCode.LeftControl) && controller.isGrounded && !Input.GetKey(KeyCode.LeftShift))
{
isCrouching = true;
playerSpeed = walkSpeed * crouchingMultiplier;
}
else
{
isCrouching = false;
}
if (isCrouching == false && isSprinting == false)
{
playerSpeed = walkSpeed;
}
if (Input.GetKey(KeyCode.Space))
{
if (groundedTimer > 0)
{
groundedTimer = 0;
verticalVelocity = jumpForce;
}
}
//Jump Mechanic
Vector3 jumpVector = new Vector3(0, verticalVelocity, 0);
controller.Move(jumpVector * Time.deltaTime);
}
}
I wanted to add that when I was referencing this 1.27 thats my y value. 1.25 is ground level, so that .02 takes approximately 1 second to settle back down to 1.25.
I was actually able to solve my own question by tinkering around. Adding this script after the input that adds jumpforce to vertical velocity, I was able to get it to get to the 1.25 the way it was supposed to be. However I have noticed that now slopes are a little wonky. Is there any way to fix slopes like how the rigidbody controller has a raycast?
Here is the script I added;
if (Input.GetKey(KeyCode.Space) && controller.isGrounded) {
}
else
{
verticalVelocity += gravity * Time.deltaTime;
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613041550im_/https://answers.unity.com/themes/thub/images/avi.jpg)