Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by jaguar1226 · Feb 07, 2019 at 04:17 PM · collider2dtriggers

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`

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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.

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image jaguar1226 · Feb 08, 2019 at 03:23 PM 0
Share

I already tried it , but it doesn't work as well

avatar image blueshark- jaguar1226 · Feb 09, 2019 at 02:08 AM 0
Share

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

avatar image jaguar1226 · Feb 09, 2019 at 12:52 PM 0
Share

Ok thank you, I tried it out and set the angular drag 0. Now it works ^^

avatar image blueshark- jaguar1226 · Feb 09, 2019 at 04:38 PM 0
Share

Glad it works! :D Good luck with the rest!

avatar image
0

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image jaguar1226 · Feb 08, 2019 at 03:23 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

103 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges