- Home /
Keep character from jumping more than once?
Hey Unity community!
I am working on a racing game, and am finalizing the drifting mechanic. I want to make the drifting work like it does in Mario Kart, in which holding the button produces a small hop, then initiates the drifting physics. I have already got the drifting physics working, but I am having an issue with the small hop. Currently, holding down the drift button produces the hop, but the vehicle continually hops until the button is let go. How do I set up my script so that the vehicle will only hop once per hold?
Here is the jump script that needs to be changed:
var speed : float = 10.0f;
var jumpForce : float = 1000.0f;
var airModifier : float = 0.2f;
var grounded : boolean;
// Update is called once per frame
function Update ()
{
if (Physics.Raycast(transform.position, -transform.up, 2))
{
grounded = true;
}
else
{
grounded = false;
}
//jump if the user pressing button AND our character is grounded
if (DriftButton.playerJump && grounded)
{
rigidbody.AddRelativeForce(transform.up * jumpForce, ForceMode.Impulse);
}
}
Any help would be great!
Answer by Lttldude · Apr 15, 2012 at 11:35 PM
Add a small wait time condition:
var speed : float = 10.0f;
var jumpForce : float = 1000.0f;
var airModifier : float = 0.2f;
var grounded : boolean;
var jumpWaitTime : float = 1.0;
private var nextJumpTime : float;
// Update is called once per frame
function Update ()
{
if (Physics.Raycast(transform.position, -transform.up, 2))
{
grounded = true;
}
else
{
grounded = false;
}
//jump if the user pressing button AND our character is grounded
if (DriftButton.playerJump && grounded && Time.time >= nextJumpTime)
{
rigidbody.AddRelativeForce(transform.up * jumpForce, ForceMode.Impulse);
nextJumpTime = Time.time + jumpWaitTime;
}
}
Thanks for your reply! This works fairly well, except it just waits for the defined time before jumping again. I really need to get it to jump once, then not jump again until the player releases the button.
Do you have the ability to recognize when DriftButton.playerjump is down or up? I'm only used to normal keyboard inputs, where they do have a key down/up. If that's the case, just set a boolean variable called canJump that has to be true for the conditional jump statement. Should true on start, false when the playerJump key goes down, and true when the key is let up (allowing for another jump to happen).
Answer by hernaez · Dec 21, 2012 at 09:44 PM
I'm working on a drift for a mario kart clone, could you help me out with the drift code?
Your answer
Follow this Question
Related Questions
Game with Multiple Vehcicles - 1 Player 1 Answer
How can I detect the time someone uses input? 1 Answer
What's wrong with this script? 4 Answers
Using a touch event for character motor 0 Answers