Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 SmooveB · Jan 23, 2013 at 01:07 AM · 2dcollisionmovementtriggerstop

Simple movement problem. Need units to stop dead on collision.

Should be a fairly simple problem, but it is giving me some trouble.

I've got some "2d" rectangular units with trigger boxes being moved around using the translate function. When they collide I need them to stop dead, but they should still be able to freely move away from each other and parallel to the other unit's edge.

Related questions have suggestions like "use triggers" which I am already doing.

My current solution is to maintain an old position from a fraction of a second ago and revert to that position OnTriggerEnter and OnTriggerStay. This results in some unsightly jerkiness, and worst of all, the tendency of two moving units to become stuck together.

How do you get objects to stop dead when colliding without sticking or jerking?

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

3 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by RetepTrun · Jan 23, 2013 at 01:46 AM

Maybe you could do a really short raycast like a milimeter infront of them. So you could have logic like

 if(someting right in front of me){
 stop
 }
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 SmooveB · Jan 23, 2013 at 07:00 PM 0
Share

Going to have to do something similar to this, but raycasting wont work because the unit is a rectangle and can move any direction, so it could catch an edge anywhere.

avatar image
0

Answer by SmooveB · Jan 23, 2013 at 07:11 PM

I ended up adding four long triggers. One to each side of the unit as children of the unit just outside the units main collision. OnTriggerEnter, these triggers stop any movment by the unit in the direction of that trigger.

So if the front trigger enters a trigger, then the main movement script gets set to something like:

horazontalMove = Mathf.Min(0,horazontalAxis)

Or if the back trigger enters a trigger then it has the main script do something like:

horazontalMove = Mathf.Max(0,horazontalAxis)

I would still like to know a solution that relies more on code and less on strange uses of the tool, but for now this works fine.

Comment
Add comment · 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
0

Answer by Loius · Jan 23, 2013 at 07:29 PM

Use rigidbody.velocity = Vector3.zero

Or if you're using triggers (which can't collide physically), you need to manually locate where they should be.

For instance, to force me and him apart such that we're exactly touching (where xWidth is the distance from the center point to the edge along the X axis):

if ( me.x + me.xWidth > him.x - him.xWidth ) me.x = him.x - him.xWidth - me.xWidth

You can use OnTriggerEnter to catch 'collisions', and then just run that for each relevant axis against the thing you hit.

Comment
Add comment · 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

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

11 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

Related Questions

How do I make animation transitions a one-way street? 1 Answer

No Collision in 2D Game 3 Answers

Object collision triggering null reference at runtime when colliding with clone of itself after being childed. 1 Answer

Script calling onCollisionEnter2D and onTriggerEnter2D while disabled 1 Answer

Stop or cancel movement if the player hits wall. 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