Can't get my character controller to jump properly (3d)
Hi I'm quite new to scripting. I've been looking around but other answers don't solve my problem. I've got my movement down with some help that I got online in a tutorial, but I can't get the player to jump.
My jumps are registering sometimes in the console. It doesn't happen with every key press though. I've been playing around with jump height and gravity but all that does is make a really sudden, high jump for about half a second that sends the player up and down immediately, no matter the settings.
Here's my script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ThirdPersonController : MonoBehaviour
{
[SerializeField] private float movementSpeed = 5f;
private float currentSpeed = 8f;
private float speedSmoothVelocity = 5f;
private float speedSmoothTime = 5f;
private float rotationSpeed = 5f;
public bool isGrounded = true;
private LayerMask layerMask;
public float rayLength;
public float gravity = 20f;
public float jumpHeight = 9f;
private Transform mainCameraTransform = null;
private CharacterController controller = null;
private CharacterAnimation animator;
private void Start()
{
controller = GetComponent<CharacterController>();
animator = GetComponent<CharacterAnimation>();
mainCameraTransform = Camera.main.transform;
}
private void Update()
{
Move();
RaycastHit();
}
private void Move()
{
Vector2 movementInput = new Vector2(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical"));
Vector3 forward = mainCameraTransform.forward;
Vector3 right = mainCameraTransform.right;
forward.y = 0;
right.y = 0;
forward.Normalize();
right.Normalize();
Vector3 desiredMoveDirection = (forward * movementInput.y + right * movementInput.x).normalized;
Vector3 gravityVector = Vector3.zero;
if(!controller.isGrounded)
{
gravityVector.y -= gravity;
if (Input.GetKeyDown("space"))
{
gravityVector.y = jumpHeight;
isGrounded = false;
Debug.Log("Jump!");
}
}
if(desiredMoveDirection != Vector3.zero)
{
transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(desiredMoveDirection), rotationSpeed);
animator._animRun = true;
}
else
{
animator._animRun = false;
}
float targetSpeed = movementSpeed * movementInput.magnitude;
currentSpeed = Mathf.SmoothDamp(currentSpeed, targetSpeed, ref speedSmoothVelocity, speedSmoothTime);
controller.Move(desiredMoveDirection * currentSpeed * Time.deltaTime);
controller.Move(gravityVector * Time.deltaTime);
}
private void RaycastHit()
{
RaycastHit hit;
Ray controllerRay = new Ray(transform.position, Vector3.down);
if (Physics.Raycast(controllerRay, out hit, rayLength))
{
if (hit.collider.tag == "environment")
{
isGrounded = true;
Debug.DrawRay(transform.position, transform.TransformDirection(Vector3.down) * rayLength, Color.yellow);
Debug.Log("I hit something!");
}
}
}
}
Any thoughts guys? Help would be greatly appreciated because I'm stuck.
Answer by NegiMox · Nov 09, 2019 at 05:30 AM
Wait,as i was reading your script at line 55 you wrote if controller.isGrounded is false meaning he is not touching ground then he should jump if space bar is pressed and below that you wrote (Input.GetKeyDown("space")) this should be (Input.GetKeyDown(KeyCode.Space))
So I just tried these changes but no positive effects have come to light.
Changing the bool and/or the key results in not being able to jump in the slightest. If I change the key press segment to "getbutton/getkey" my character can jump high but will keep rising upwards when holding space.
Which would imply that your "isGrounded" code, to prevent this, is not working.
The character goes up even if you are in air and you press the key right? this is so bcuz you wrote that if the !controller.isGrounded which mean if character isnt touching the ground that is why you jump when you are in air.
Okay, makes sense. So I remove the ! to make the code read "if grounded: jump", but what about the actual jump? Changing that segment makes it so that I cant jump at all
You mean if you write if controller.isGrounded then you are not able you jump? The problem here might be of controller.isGrounded function,you should create your detection it is fairly simple you have to just cast a ray from the player. This video will tell you how to do so: https://www.youtube.com/watch?v=j$$anonymous$$u-p_Oy1p$$anonymous$$ or you can search on your own by searching on how to use raycast in unity for jumping and look for ur like this will fix your problem.
Thanks man but I have a ray cast method at the bottom. I dont think theres a problem with it? I've got a fairly good understanding of ray casts now I think
Good for you.It fine that the ray is pointing downward as it will collide with them so the object would obviously be beneath him thats why.