• 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 Regan · Sep 21, 2012 at 07:17 PM · collisionpathfindingparty

When instantiating an object inside of something, how can I move it to the outside of the object?

Alright, to be more specific I've been writing a formations system for a party of players. The problem arises when I click too close to an obstacle, and one of the markers for the formation clips inside of an object like so:

marker


So basically what I need to happen is if these markers clip into an object, I need them to adjust themselves so they're outside the object. I've wrote several scripts myself, using raycasts to detect collisions, and then adjusting them each frame based on which rays are touching the object. However these don't work within one frame. Ideally i'd like to be able to make this happen within one frame so that as soon as I click, my characters get the updated locations of the markers, rather than their original locations. Any suggestions on where I should start?

markers.png (43.6 kB)
Comment
Add comment · Show 3
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 thesfid · Sep 21, 2012 at 07:57 PM 0
Share

I'm no pro, so I'm posting as a comment ins$$anonymous$$d of an answer. I'm sure there's a more elegant solution out there, but maybe this will be a good starting point.

$$anonymous$$y first thought is to use meshcolliders to detect intersections. If an object is intersecting/clipping, you could move the marker an arbitrary distance and direction through code.

avatar image Regan · Sep 21, 2012 at 08:03 PM 0
Share

Thats actually similar to what I did. I used several mesh markers corresponding to 'north', 'south', 'east', and 'west'. And then moved the markers in the appropriate direction based on what was touching the object.

The problem is theres no way (as far as I know) to tell how far inside the object the marker might be. Which is why I incremented the position a small amount in each frame. I'd like to be able to do that in one frame if possible, I'm not sure how though.

avatar image thesfid · Sep 21, 2012 at 09:43 PM 0
Share

You could use mesh.bounds.size (Vector3 size.x size.y, size.z) to deter$$anonymous$$e the $$anonymous$$imal distance to move. $$anonymous$$ight end up having to double that distance to make sure the marker doesn't continue to clip in a different spot, but probably not if you've figured out which direction you need to move the marker.

http://docs.unity3d.com/Documentation/ScriptReference/Bounds.html

$$anonymous$$aybe there's something in there that you can use. Sorry for not being more helpful.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Muuskii · Sep 21, 2012 at 08:02 PM

Does it help any to use a Physics.SphereCast at the target location first? Then use one of the contact points to determine the direction and length that we must move the target location before restarting the process until we find a clear spot?

If you assign a very large radius to the sphere you can define a quiet a bit of clearance, but it will probably take longer to find a clear spot.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

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

Isometric character collision 1 Answer

help with AI avoidance script 0 Answers

How do I make objects disappear as they are touched? 1 Answer

Draw grid and check colisions with other objects for every cell 1 Answer

How to make whole navmesh agent avoid carves rather then center of it? 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