- Home /
I can't do that my character jumps while running
Hi, I made a first person controller script but I can't do that my character jumps correctly. When I'm using the joystick stick to move + pressing the button to run + pressing the button to jump, the character loses all the speed and it jumps in 90 degrees, then it falls to the ground and continue running. What I want is that when he jumps, he keeps the same speed that he had when he was in the ground so the jump is not in 90 degrees but it has an agude angle.
This is the script:
public class FirstPersonController : MonoBehaviour { // Public variables public float speed; public float mouseSensivity = 6f; public float jumpSpeed = 4f;
// Private variables
private CharacterController cc;
private float upDownRange = 40f;
private float verticalRotation = 0f;
[SerializeField] private float verticalVelocity = 0f;
void Start ()
{
cc = GetComponent <CharacterController> ();
Cursor.lockState = CursorLockMode.Locked;
}
void Update ()
{
// Rotation
float horizontalRotation = Input.GetAxis ("Joystick X") * mouseSensivity;
transform.Rotate (0f, horizontalRotation, 0f);
verticalRotation -= Input.GetAxis ("Joystick Y") * mouseSensivity;
verticalRotation = Mathf.Clamp (verticalRotation, -upDownRange, upDownRange);
Camera.main.transform.localRotation = Quaternion.Euler (verticalRotation, 0f, 0f);
// Movement
float horizontalMove = Input.GetAxisRaw ("Horizontal");
float verticalMove = Input.GetAxisRaw ("Vertical");
verticalVelocity += 2.5f * Physics.gravity.y * Time.deltaTime;
speed = 8f;
if (cc.isGrounded)
{
verticalVelocity = Physics.gravity.y/10;
if (Input.GetButtonDown ("Jump"))
{
verticalVelocity = jumpSpeed;
}
if (Input.GetButton ("Sprint"))
{
speed = 14f;
}
}
Vector3 move = new Vector3 (horizontalMove, verticalVelocity, verticalMove);
move.Normalize ();
move = transform.rotation * move * speed;
cc.Move (move * Time.deltaTime);
if (Input.GetKeyDown (KeyCode.Escape))
Application.Quit();
}
}
Someone of you know how to do that?
Your answer
Follow this Question
Related Questions
Player Inconsistent Jumps 0 Answers
Jumping not always work 2 Answers
jump with character controller best way? 1 Answer
How do I script so my character Jumps farther when Running? 1 Answer
Jumping with Character Controller?! 1 Answer