- Home /
jump on collision enter
i want to make a zelda style jumping system where when you come to the edge of a platform you automatically jump. i figure the best way to do this is to make my player jump when he enters a trigger.
if (jumpInput && !crouchInput && okToRepeatJump && animationGrounded) {
// jump!
onGround = false;
velocity = moveInput * airSpeed;
velocity.y = jumpPower;
thats my jump code at the moment, but i cant figure out how to do the ontriggerenter stuff. any help is appreciated
Do you mean, ins$$anonymous$$d of the player being able to press "Space" to jump, hitting the trigger is pretty much like the "Space" button? If this is what you mean I can try whip up a script.
Answer by NickP_2 · May 31, 2014 at 02:58 PM
Put the jump code in a function:
(c#)
public void Jump()
{
onGround = false;
velocity = moveInput * airSpeed;
velocity.y = jumpPower;
}
Now attach a script to the edge collider, and check the "is trigger" box in the inspector of the collider.
The script:
//this function get's called when another collider enters this collider, one of each colliders must have a rigidbody or characterController
void OnTriggerEnter(Collider col)
{
//You can add tags in the upper left side of the inspector of any object
//maybe you have other moving objects, so check the tag of the entering object for player
if(col.transform.tag == "Player")
col.GetComponent<PlayerScript>().Jump();//where "PlayerScript" is the name of the script attached to the object which represents the player and which contains the Jump() function
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613150348im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
activate gui on trigger enter 1 Answer
Collision between objects? 2 Answers
OnTriggerEnter not working all the time. 2 Answers
Trigger script does not work. 1 Answer