- Home /
How to double jump
Hi, ive been scouring the internet left and right to put double jumps into my code,i'm making a 3D platformer, using a 3rd person controller and c#. I would like to know what additional script for the double jumping ill have to use and where abouts to include it.
Thanks. I'm quite new to this.
using UnityEngine; using System.Collections;
public class PlayerController : MonoBehaviour {
public CharacterController controller;
public GameObject projectile;
Vector3 movementVector = Vector3.zero;
//movement variables
public float moveForce = 0;
public float moveDamping = 0.5f;
public float moveForceToAdd = 1f;
public float movementSpeed = 2f;
//jumping variables
public float jumpForce = 1.0f;
public float jumpDamping = 0.5f;
public float jumpForceToAdd = 100f;
int jumpCount = 1;
bool grounded = false;
// Use this for initialization
void Start () {
int result = AddNumber (2, 3);
Debug.Log (result);
}
// Update is called once per frame
void Update ()
{
if (Input.GetKeyDown (KeyCode.Q))
{
Instantiate (projectile, transform.position + transform.forward * 2, transform.rotation);
}
movementVector = new Vector3 (Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
if (Input.GetAxis ("Vertical") != 0 || Input.GetAxis ("Horizontal") != 0)
{
moveForce += moveForceToAdd;
}
if (Input.GetKeyDown (KeyCode.Space))
{
if (controller.isGrounded)
{
jumpForce += jumpForceToAdd;
}
}
movementVector *= moveForce;
movementVector += Physics.gravity;
movementVector += new Vector3 (0, jumpForce, 0);
gameObject.GetComponent<CharacterController>().Move (movementVector * Time.deltaTime);
jumpForce *= jumpDamping;
moveForce *= moveDamping;
}
int AddNumber (int a, int b)
{
return a + b;
}
} `
Answer by fafase · May 04, 2014 at 07:35 AM
private bool doubleJump = false;
if (Input.GetKeyDown (KeyCode.Space))
{
if (controller.isGrounded)
{
doubleJump = true;
jumpForce += jumpForceToAdd;
}else if(doubleJump){
doubleJump = false;
jumpForce += jumpForceToAdd;
}
}
Also, this video http://unity3d.com/learn/tutorials/modules/beginner/2d/2d-controllers has something about it.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
C# Make something happen for X amount of Seconds. 2 Answers
My code dont work on a MLAgents, Unity 2020.2.7f 0 Answers
How can I reset a rigidbody? 1 Answer
Adding collider to 3D model 2 Answers