- Home /
I aren't able to jump. I find no mistake in the script
Hi guys, I have a little question. I want to make a third person controller. I wrote the script, made a Cinemachine and tried to play the game. I was able to walk, to sprint but i wasn't able to jump more than one time in one game. Can anybody help me? Have I maybe a script mistake Thanks! PS: Sorry for my bad English
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class ThirdPersonController : MonoBehaviour
{
public Camera MyCamera;
public float Speed = 5f;
public float SprintSpeed = 8f;
public float RotationSpeed = 15;
public float JumpSpeed = 5f;
CharacterController MyController;
float mDesiredRotation = 0f;
bool mSprinting = false;
float mSpeedY = 0;
float mGravity = -9.81f;
bool mJumping = false;
// Start is called before the first frame update
void Start()
{
MyController = GetComponent<CharacterController>();
}
// Update is called once per frame
void Update()
{
float x = Input.GetAxisRaw("Horizontal");
float z = Input.GetAxisRaw("Vertical");
if(Input.GetButtonDown("Jump") && !mJumping)
{
mJumping = true;
mSpeedY += JumpSpeed;
}
if(!MyController.isGrounded)
{
mSpeedY += mGravity * Time.deltaTime;
}
else if(mSpeedY < 0)
{
mSpeedY = 0;
}
if(mJumping && mSpeedY < 0)
{
RaycastHit hit;
if(Physics.Raycast(transform.position, Vector3.down, out hit, .5f, LayerMask.GetMask("Default")))
{
mJumping = false;
}
}
mSprinting = Input.GetKey(KeyCode.LeftShift);
Vector3 movement = new Vector3(x, 0, z).normalized;
Vector3 rotatedMovement = Quaternion.Euler(0, MyCamera.transform.rotation.eulerAngles.y, 0) * movement;
Vector3 verticalMovement = Vector3.up * mSpeedY;
MyController.Move((verticalMovement + (rotatedMovement * (mSprinting ? SprintSpeed : Speed))) * Time.deltaTime);
if (rotatedMovement.magnitude > 0)
{
mDesiredRotation = Mathf.Atan2(rotatedMovement.x, rotatedMovement.z) * Mathf.Rad2Deg;
}
Quaternion currentRotation = transform.rotation;
Quaternion targetRotation = Quaternion.Euler(0, mDesiredRotation, 0);
transform.rotation = Quaternion.Lerp(currentRotation, targetRotation, RotationSpeed * Time.deltaTime);
}
}![alt text][1]
are you sure you are pressing the correct button? does your mJumping
ever change to true?
Hi, maybe I'm missing out on something, but as far as I understand this, the raycast is to check if you are grounded right? Can't you just use CharacterController.isGrounded to check if the player is grounded when trying to jump? Maybe that works out a bit better than the raycasting :D Also, is your player on the 'Default' layer too? Because that would mean that the raycast also hits the player.
Answer by tannergilliland9 · Apr 23, 2021 at 05:58 PM
Something might be wrong with your raycast on line 47, Can you add Debug.Log("hi") or something and tell us if it gets run?
@tannergilliland9 , thanks for the info. I've done it and there was nothing in the console. Have you any idea where is the mistake?
Follow this Question
Related Questions
How to jump multiple times to mid air? 3 Answers
Character doesn't jump sometimes 1 Answer
How to jump using a CharacterController and NavMeshAgent 0 Answers
I cant make my Character jump. 0 Answers
How do you make a characterController jump automatically when it reaches the edge of a platform? 0 Answers