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 Courtneyssg · Feb 24, 2020 at 07:28 PM · uimovementcollisiondetection2d collision

Looking for a clean bug-free solution to my issue with collisions on a moving UI object with other UI objects

A ball-like UI game object is supposed to bounce around the menu screen. It can collide with the screen edges or several other UI game objects (example: buttons).

The following code is how I complete this collision on the screen edges:

 void Awake()
 {
     v = Quaternion.AngleAxis(Random.Range(0.0f, 360.0f), Vector3.forward) * Vector3.up; //calculate a random angle to start moving the ball-like UI object when the menu loads
 }
 void Update()
 {

     transform.position += v * _speed * Time.deltaTime; //moving the UI game object

     if (transform.position.x >= _right + _canvasX)
     {
         v.x *= -1.0f; // if the game objects x position is greater than the boundary of the right side of the screen invert the x movement (I don't do this to the left side because there is a UI game object blocking the left side in which the moving ball-like UI game object isn't supposed to be able to pass through)
     }

     if(transform.position.y >= _top + _canvasY || transform.position.y <= _bottom + _canvasY)
     {
         v.y *= -1.0f; //do the same for the top and bottom of the screen as I did above, but invert the movement on y axis.
     } 

 }

This works perfectly however it doesn't for collision with other objects.

I have tried using box colliders around the other UI objects the moving object is supposed to collide with and use OnCollisionEnter2D. However, sometimes the moving object still passes right through the collision zone. I'm thinking it does this because it is only detecting the collision as the game object enters the collider. I'm thinking the issue might be that it's only doing this once as the object collides. If the object happens to still be in the collision zone after movement is inverted no more inversion is applied and it continues on it's trajectory through the game object. I tried solving this issue by removing the box colliders and writing out the collision restrictions similar to how I have done for the screen edges however this seems to yield the exact same result.

The collisions work 90% of the time but I don't want there to be any chance of the object leaving the screen entirely. Any suggestions?

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by denisbodrug05 · Feb 24, 2020 at 07:43 PM

Do not try to identify the corners. Use Vector 2, because is 2D, and use sprites instead of ui elements. Add a collider as a border (maybe 2, one a little bigger, and one a bit smaller for no bugs) and a bigger collider that is set on is triggered true witch will colide and maybe apply some force.

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 Courtneyssg · Feb 24, 2020 at 08:56 PM 0
Share

I haven't been identifying corners with collision I've been identifying edges parallel to x and y axes. I just called them UI elements in the respect that they are the elements that make up the UI of the main menu. Inside of the canvas they are actually sprites. Is this what you meant, or did you mean remove them from UI completely? If that is what you meant it would be on a different layer and fail to collide with the UI buttons and such.

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

251 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 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 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

UI buttons to move character? 0 Answers

Weird shaky player behavior (moving through colliders) 0 Answers

2D Collider Isnt working 1 Answer

Using UI buttons for Input Manager 0 Answers

How can I make the player stop moving when a UI dialogue box is active? 2 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