- Home /
Disable function OnTriggerExit?
Hi i am a complete beginner at programming. I made a sphere that can jump and move horizontally.
public class PlayerControler1 : MonoBehaviour {
public float speed;
public float JumpSpeed;
void FixedUpdate ()
{
movement ();
}
void movement ()
{
float h = Input.GetAxis ("Horizontal") * speed;
rigidbody.AddForce (h, 0f, 0f);
Jump ();
}
public void Jump ()
{
if (Input.GetButtonDown ("Jump"))
rigidbody.AddForce (0f, JumpSpeed, 0f);
}
Now the problem is i want to make it jump only when its grounded so i made a child empty gameobject inside my sphere and added a sphere collider in it and made it trigger, i made it a little bigger than the player sphere collider.
I made a script in it and did the inheritance thing. Now i want it to disable the Jump function when the trigger collider exits the ground.
So i made this
public class JumpDead : PlayerControler1 {
void OnTriggerExit (Collider Ground)
{
Jump (false);
}
But when i return to play the game it gives me an error.
Assets/JumpDead.cs(8,17): error CS1501: No overload for method Jump' takes
1' arguments.
Can someone explain me what im doing wrong and how to do it right ?
Answer by Ferb · Jan 21, 2014 at 06:50 PM
The line:
Jump(false);
is trying to send the argument 'false' to your 'Jump' function, which as it says, doesn't take any arguments (the brackets after 'Jump' are empty).
A better idea would be to have a variable - maybe called 'canJump', and just check that when you want to jump. So add a line at the top saying:
private bool canJump = true;
(bool means it's a type of variable that is either true or false) and make your Jump code:
if (Input.GetButtonDown ("Jump"))
if(canJump)
rigidbody.AddForce (0f, JumpSpeed, 0f);
Now your collider doesn't need to call any function, it just needs to set 'canJump' to either true or false. I'd recommend you just make those changes to your original PlayerControler class for now though, I don't see any reason you need to inherit a new class, that's just complicating things. And if you set canJump to false in OnTriggerExit, remember to set it to true again in OnTriggerEnter. If you have more trouble, try watching the tutorial here, which covers the subject of jumping in a 2D game. http://unity3d.com/learn/tutorials/modules/beginner/2d/2d-controllers
i dont know what im doing wrong it still jumps while on air
Answer by skalev · Jan 21, 2014 at 06:37 PM
Calling Jump(false) doesn't disable your function. It calls it with false as a parameter.
Since you have no function that is called Jump and takes one parameter of type bool, it cannot compile.
what you want is to have a bool flag that is checked.
so under JumpSpeed you add:
public bool canJump = true;
in the jump function you change the condition to:
if (Input.GetButtonDown ("Jump") && canJump)
And finally in OnTriggerExit(), you change the call to be:
canJump = false;
As a note this is really not the best solution what so ever, but this is what needs to change to make it work.