Hi. I’m trying to learn unity and I’ve created a CharacterController script move, jump and crouch.
everything works very fine except that jumping is not smooth at all !
when i first press jump it will move instantly upwards and then falls smoothly; and its very annoying tbh.
I really do need help on this because I’ve searched everywhere and didn’t find any solutions.
My script :
using UnityEngine;
public class PlayerMovementScript : MonoBehaviour
{
public CharacterController controller;
public Transform player;
public float gravityMultiplier = 1f;
public float speed = 8f;
public float jumpHeight = 2.4f;
Vector3 movement;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
float x = Input.GetAxis("Horizontal");
float z = Input.GetAxis("Vertical");
// Crouch [ i dont have a character and its just a capsule and offline , so i just shrink it for crouch xD ]
if(Input.GetButtonDown("Crouch")) // Added a new axes called crouch with left shift
{
speed /= 3;
jumpHeight /= 1.5f;
player.transform.localScale *= 0.5f;
}
if(Input.GetButtonUp("Crouch"))
{
speed *= 3;
jumpHeight *= 1.5f;
player.localScale = player.transform.localScale / 0.5f;
}
movement = transform.right * x + transform.forward * z;
movement *= speed;
// Applying Jump
if (Input.GetButtonDown("Jump") && controller.isGrounded)
{
movement.y = jumpHeight;
}
// Applying Gravity
if (controller.isGrounded == false)
{
movement.y += Physics.gravity.y * gravityMultiplier;
}
// Applying Movement
controller.Move(movement * Time.deltaTime);
}
}
movement.y = jumpHeight;
Sets the amount to jump very high initially.
You’re asking Unity to set the jump height to an arbitrary value and then hoping update and Time.deltaTime will smooth it up.
You’ve not provided anything that looks like a speed modifier in your // Applying Movement section so unity is clocking off frames till it reaches target and you see a “jitter jump” to that location.
You either need to jump to jumpHeight with a slerp function or scale the Time.deltaTime by some expected speed.
Please check this documentation on how to perform the jump using a characterController:
I think the problem is that you’re resetting the movement vector every frame, so your jump code will set the jump height for 1 frame and then be overwritten.
Notice in that documenation how it will only set the horizontal and vertical movement when the character is grounded.
The only thing that happens to the movement vector while not grounded is gravity calculations.
If you want to enable horizontal movement while mid-air, you need to get the current movement vector and add the horizontal movement vector to it.
I can’t speak to the above question but it would probably be best for you to start a fresh thread yourself, as that is how the forum rules specify, and explain your issue clearly.
How to report your problem productively in the Unity3D forums:
This code worked for me give it a try public CharacterController characterController; private Vector3 velocity; public float Speed = 2f; public float jump = 10f; public float Gravity = -9.8f; void update() {
// for movement float horizontal = Input.GetAxis(“Horizontal”) * Speed; float vertical = Input.GetAxis(“Vertical”) *Speed; Vector3 move = transform.right * horizontal + transform.forward * vertical; characterController.Move(move * Speed * Time.deltaTime);
// for jump if (Input.GetButtonDown(“Jump”) && transform.position.y< -0.51f) // (-0.5) change this value according to your character y position + 1 { velocity.y = jump; } else { velocity.y += Gravity * Time.deltaTime; } characterController.Move(velocity * Time.deltaTime); }
There is a change that must be made in the code of this link (Unity - Scripting API: CharacterController.Move) which is in this part:
if (groundedPlayer && playerVelocity.y < 0)
{
playerVelocity.y = valueTest;
}
where valueTest is some value less than zero that should be tested by you. If you leave it zero, it crashes when jumping on ramps.