So, I’m having a bit of an annoying issue right now. I am using EasyTouch for touch screen controls BTW.
One of the touch controls is a button. When you press the button, it sends a message. Then, in my Player Jump script, if the message is received, the player will jump. In simpler terms, when I press the button, the player jumps. If I press the button then release. The player jumps like he should. However, if I hold the jump button, the player will sort of fly? He’ll sort of go straight upwards, almost like an infinite jump…
var JumpHeight = 8;
var PlayerJump : boolean = false;
//Message received will result in this function being "activated"
function Jump ()
{
PlayerJump = true;
}
function Update ()
{
if (PlayerJump == true)
{
rigidbody.velocity.y = JumpHeight;
//My attempt at making the player not fly straight up //into the air...
PlayerJump = false;
}
}
By the way, I’m brand new to Unity, so easy to understand explanations please
Thanks everyone!
That is because the Update function is called every frame, so your code basically says, if the jump button is being pressed, add some force making the player go up. Setting PlayerJump to false isn’t working because every frame the jump button is held down, you’re adding more velocity to the player. You would first have to check if the player is grounded and only add the velocity if that is true
Make an empty Gameobject called mytrans and another called groundCheck. Make them children of your player. Replace your script with the code below and assign mytrans to the myTrans variable in the inspector and make groundCheck the groundTag variable in the inspector. Position myTrans at 0,0,0 or the center of the player and make groundCheck a little bit below the players feet. That should work, if it doesn’t you can comment below.
I edited the code so that the boolean isGrounded is set to the return value of a linecast. this means that whenever the line that is drawn from mytrans to groundCheck is hitting something, the value returns true, making the isGrounded variable true. Then when the jump button is pressed and the isGrounded variable is true, your player will jump.
var JumpHeight = 8;
var PlayerJump : boolean = false;
var isGrounded : boolean;
var myTrans : Transform;
var groundTag : Transform;
//Message received will result in this function being "activated"
function Jump ()
{
PlayerJump = true;
}
function Update ()
{
isGrounded = Physics2D.Linecast (myTrans.position, groundTag.position);
if (PlayerJump == true && isGrounded)
{
rigidbody.velocity.y = JumpHeight;
}
}
Okay so I fixed my issue. I temporarily made another script for the jumping exclusively.
static var Jump : boolean = false;
var JumpHeight = 8;
var DistToGround : float;
function Start ()
{
DistToGround = collider.bounds.extents.y;
}
function MoveJump ()
{
Jump = true;
}
function Update ()
{
if (Jump == true && IsGrounded ())
{
rigidbody.velocity.y = JumpHeight;
JumpFalse ();
}
}
function IsGrounded ()
{
return Physics.Raycast(transform.position, -Vector3.up, DistToGround + 0.1);
}
function JumpFalse ()
{
yield WaitForSeconds (1);
Jump = false;
}