Double Jump Not Working Good
My double jump script works however after a while, to be more specific after jumping around my scene, suddenly i can't double jump anymore, and only 1 jump works then. Can someone check whats wrong? I am also trying to get my FPS player to walljump, and gain jumping movement speed while jumping, which slows down after i stop jumping.
Also my air control is massive and i notice there is no gravity when i jump from boxes i jst fall down, can someone help, or point me to what am i doing wrong? Here is my movement script by following tutorials etc. I am trying to get feeling as between quake and unreal tournament genres.
using UnityEngine;
using System.Collections;
[RequireComponent(typeof(CharacterController))]
public class FPSController : MonoBehaviour
{
public float movementSpeed = 5.0f;
public float jumpSpeed = 8.0f;
int jumpCount = 0;
int maxJumps = 1;
float verticalVelocity = 0;
CharacterController characterController;
void Awake () {
Screen.lockCursor = true;
characterController = GetComponent<CharacterController>();
}
void Update()
{
float forwardSpeed = Input.GetAxis("Vertical") * movementSpeed;
float sideSpeed = Input.GetAxis("Horizontal") * movementSpeed;
verticalVelocity += Physics.gravity.y * Time.deltaTime;
if( characterController.isGrounded && Input.GetButtonDown("Jump") ) {
verticalVelocity = jumpSpeed;
jumpCount++;
}
if(jumpCount==maxJumps && !characterController.isGrounded && Input.GetButtonDown("Jump") ) {
verticalVelocity = jumpSpeed;
jumpCount=0;
}
Vector3 speed = new Vector3( sideSpeed, verticalVelocity, forwardSpeed );
speed = transform.rotation * speed;
characterController.Move( speed * Time.deltaTime );
}
}
Answer by -hiTo- · Aug 24, 2013 at 03:39 PM
I see a problem with your if-statements.
What if you only jump once, then you're back on the ground. Then you do it again. Then you do it again.
Your jumpCount would be 3, and your second if-statement would never run.
Set your jumpCount in your first if statement to 0, and it should solve the problem.
Just a tip: If you use a bool, you would only need 1 variable, instead of 2 integers. Example:
private bool HasDoubleJumped = false;
private void Update()
{
if (characterController.isGrounded && Input.GetButton("Jump"))
{
verticalVelocity = jumpSpeed;
HasDoubleJumped = false;
}
else if (!HasDoubleJumped && !characterController.isGrounded && Input.GetButton("Jump"))
{
verticalVelocity = jumpSpeed;
HasDoubleJumped = true;
}
}
Thank you, it worked! I was thinking all day how to do this. And you solve it in few $$anonymous$$utes. Here is solution.
if(characterController.isGrounded && Input.GetButtonDown("Jump") ) {
verticalVelocity = jumpSpeed;
jumpCount=0;
}
if(jumpCount<maxJumps && !characterController.isGrounded && Input.GetButtonDown("Jump") ) {
verticalVelocity = jumpSpeed;
jumpCount=1;
}