- Home /
Problem with the collider trigger,How to use colliders with trigger?
I'm new to Unity and currently making a simple 2D game with a turtle that moves up and down. I don't want it to go out of the water at the top or out of the game window at the Bottom. When I use the normal Colliders, it starts shaking and Spinning and eventually gets kicked out of the game window. Therefore I want to use triggers, but somehow my Code doesn't work:
public void OnTriggerEnter(Collider2D collision)
{
if (collision.gameObject.tag == "background" && rigidbodyComponent.position.y < -5) {
if (movement.y < 0) {
movement.y = 0;
rigidbodyComponent.velocity = movement;
}
}
if (collision.gameObject.tag == "background" && rigidbodyComponent.position.y > -5)
{
if (movement.y > 0)
{
movement.y = 0;
rigidbodyComponent.velocity = movement;
}
}
}
The turtle just passes through the Colliders :/ Thanks a lot for your help!!! :)
,Hey! I'm new to Unity and currently making a small game with a turtle that can only swim up and down. I want it to stop at the top of the water and at the Bottom of the screen, but when I use the Colliders, it doesn't really work and starts shaking when it touches the Collider. In the end, it gets thrown out of the game window… Therefore I thought that using the trigger and just making the turtle stop moving up could work, but it doesn't. Here is my Code:
public void OnTriggerEnter(Collider2D collision)
{
if (collision.gameObject.tag == "background" && rigidbodyComponent.position.y < -5) {
if (movement.y < 0) {
movement.y = 0;
rigidbodyComponent.velocity = movement;
}
}
if (collision.gameObject.tag == "background" && rigidbodyComponent.position.y > -5)
{
if (movement.y > 0)
{
movement.y = 0;
rigidbodyComponent.velocity = movement;
}
}
}
Thanks for your help! :)`enter code here`
Answer by blueshark- · Feb 07, 2019 at 04:37 PM
You should not use triggers as walls since they're triggers, you could just give the objects some kind of collider, same with the player and they shouldn't let each other pass. The reason your code doesn't work is because you're only checking for the frame they enter each other. OnTriggerEnter only checks for the frame you enter, if you want to use triggers and keep checking while they're inside you should use OnTriggerStay instead.
Also OnTriggerStay or any trigger command take a bit to process and run the code so you may touch the wall, go a bit inside of it and won't be able to come back out. Try this:
Put a Box/Sphere/$$anonymous$$esh collider or any favorable collider on the walls and turtle
$$anonymous$$ake sure all of the object's have a Rigidbody and make the walls static (can't be moved) or kinematic (not driven by the physic's engine but by Transform).
See if they collide and stop each other
Ok thank you, I tried it out and set the angular drag 0. Now it works ^^
Answer by Chimer0s · Feb 08, 2019 at 07:57 AM
As xBlackhawk mentions, OnTriggerEnter is only called when first making contact with the trigger, so any subsequent frame while inside the trigger will not set the velocity to 0. To have this happen every frame, you'll want to have it checked in OnTriggerStay.
I'm curious, though, how you're moving the turtle. Do you assign its velocity every frame? If so, and if you're using Update to do so, that could be the reason your original colliders weren't working. Physics calculations are handled in the FixedUpdate function and should be set there. It's also important to make sure you handle the movement of a rigidbody with physics (which I assume you're doing in the rest of your code, going by the small snippet). If you're ever trying to move a rigidbody by it's transform and not through the use of velocity/forces, you'll run into collision issues as well.
I already tried out OnTriggerStay, it doesn't work as well and I'm also using FixedUpdate for the physics. Is there an option to prevent objects from bouncing back when they touch a collider(without trigger)?
Your answer
Follow this Question
Related Questions
OnTrigger events not working 1 Answer
How to enable passing through collider if speed is high enough at collision point 0 Answers
Issue with Raycasts in Unity 2D 2 Answers
Performance of player detection: Colliders/Triggers vs OverlapCircle 2 Answers
How do I make a bridge in game over an existing collision? 0 Answers