So I have this script where I use a timer to prevent jumping, but this is sort of overrided if you jump at a high block and have enough fall time to get the timer to zero, also it is weird for me if I have to wait a second or two even if I’m on the ground and needing to jump. What I want to do is have something check for the ground, and then set a variable to true if im on the ground. If that variable is true, then I can jump. Also I have heard of linecasting, but I don’t understand how it works, so if that is your solution can you tell me what the code does along with how linecasting works, because I don’t get the Unity description of it.
Here is the script I have right now called “Movement Test”:
#pragma strict
var movementSpeed : float = 5.0f;
var jump : float = 0;
var jumpSpeed : float = 5;
var jumpTimer : float = 0;
function Start () {
}
function Update () {
if (jump == 1){
jumpTimer = jumpTimer + 1;
if (jumpTimer >= 85) {
jumpTimer = 0;
jump = 0;
}
}
// This is how the keys work
if(Input.GetKey("a") || Input.GetKey("left")){
transform.position -= Vector3.right * movementSpeed * Time.deltaTime;
} else if(Input.GetKey("d") || Input.GetKey("right")){
transform.position += Vector3.right * movementSpeed * Time.deltaTime;
}
if(Input.GetButtonDown("Jump") || Input.GetKey("w")){
if (jump==0) {
rigidbody2D.velocity.y = jumpSpeed;
jump = 1;
}
}
}