- Home /
How to Double Jump??? Whats Wrong?
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class TpCharacterMovement : MonoBehaviour { public CharacterController cController; public Transform cam; public Transform groundCheck;
public float movementSpeed = 6;
public float turnSmoothTime = 0.1f;
public float groundDistance = 0.1f;
public float jumpHeight = 3.0f;
private float doubleJjumpHeight = 3.0f;
private int cJump = 0;
private float gravity = -39.00f;
private float turnSmoothVelocity;
private Vector3 velocity;
public LayerMask groundMask;
private bool isGrounded;
private bool canDoubleJump;
// Update is called once per frame;
void Update()
{
isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, groundMask);
if (isGrounded && velocity.y < 0)
{
//velocity.y = -2.0f;
}
float horizontal = Input.GetAxisRaw("Horizontal");
float vertical = Input.GetAxisRaw("Forward");
//normalized is to not over speed when press for exemple W and D. Cause its to Axis at the same time;
Vector3 direction = new Vector3(horizontal, 0f, vertical).normalized;
if (direction.magnitude >= 0.1f)
{
float targetAngle = Mathf.Atan2(direction.x, direction.z) * Mathf.Rad2Deg + cam.eulerAngles.y;
float angle = Mathf.SmoothDampAngle(transform.eulerAngles.y, targetAngle, ref turnSmoothVelocity, turnSmoothTime);
transform.rotation = Quaternion.Euler(0f, angle, 0f);
Vector3 moveDirection = Quaternion.Euler(0f, targetAngle, 0f) * Vector3.forward;
// Apply the movement calculations;
cController.Move(moveDirection * movementSpeed * Time.deltaTime);
}
if (Input.GetButton("Jump") && isGrounded)
{
// Real Math Equasion formula to calculate: Ammount of velocity to jump a certain height;
velocity.y = Mathf.Sqrt(jumpHeight * -2.0f * gravity);
canDoubleJump = true;
}
if (Input.GetButton("Jump") && canDoubleJump)
{
velocity.y = Mathf.Sqrt(doubleJjumpHeight * -2.0f * gravity);
canDoubleJump = false;
}
//Simulate Gravity to the Character;
velocity.y += gravity * Time.deltaTime;
cController.Move(velocity * Time.deltaTime);
}
}
Can you please upload your character controller script. also, is there any errors or does it just not double jump?
It looks like the double jump is being consumed on the initial jump. Try adding && ! isGrounded to the if statement
if (Input.GetButton("Jump") && canDoubleJump)
Your answer
![](https://koobas.hobune.stream/wayback/20220613043457im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
BoxCast to tell if player is grounded 0 Answers
Separate Objects after Mouse Clicking in Unity 5.6.4 1 Answer
Destroying projectile on collision with every object. 1 Answer
Clear gaps/cuts in trail renderer 0 Answers
Multiple Cars not working 1 Answer