• 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
Question by sacredgeometry · Feb 26, 2010 at 03:48 PM · collisionraycastplayeraisteering

Determining rotation for collision avoidance?

Hi i was wondering what the best way to determine w$$anonymous$$ch way a character should rotate when it its ray returns a $$anonymous$$t.

I have thought about casting two rays so that the one least close to the collier determines the direction, but i cant figure out how to code the ray direction.

any help is very appreciated.

Thanks

Brian

alt text

// C# code version of JS provided by Motionreactor below RaycastHit $$anonymous$$t; Ray ray = Ray(gameObject.transform.position, Vector3.forward);

     if (Physics.Raycast(ray, $$anonymous$$t, 10))
     {
         Debug.Log($$anonymous$$t.distance);
         Debug.DrawLine(ray.origin, $$anonymous$$t.point);
     }

Comment
Ricardo
Hellium

People who like this

0 Show 0
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
Best Answer

Answer by Motionreactor · Feb 26, 2010 at 04:37 PM

You can cast a ray in a specific direction like t$$anonymous$$s:

var $$anonymous$$t : RaycastHit;
var ray : Ray = Ray(gameObject.transform.position, Vector3.forward);
if(Physics.Raycast(ray, $$anonymous$$t, 10))
{
    Debug.Log($$anonymous$$t.distance);
    Debug.DrawLine(ray.origin, $$anonymous$$t.point);
}

Vector3.forward could be any Vector direction you wish.

Comment
sacredgeometry

People who like this

1 Show 5 · 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 sacredgeometry · Feb 26, 2010 at 05:05 PM 0
Share

Sorry my fault, i've got snow blindness from staring at too much code.

avatar image Motionreactor · Feb 26, 2010 at 05:06 PM 0
Share

:) heh we just went through deleting comments at the same time... so yeh, no worries

avatar image Motionreactor · Feb 26, 2010 at 05:35 PM 0
Share

Oh, btw.. I remembered this awesome reference on autonomous steering: http://www.red3d.com/cwr/steer/

avatar image sacredgeometry · Feb 26, 2010 at 09:00 PM 0
Share

Thank you so much for the link that will help lots, currently my little guy is getting stuck in corners :D... an easy fix but i think theres a lot worse to come.

avatar image Motionreactor · Feb 27, 2010 at 02:38 AM 0
Share

heh, yeh I've done a little bit of this kind of thing before and experienced the same thing. There's always problem cases like that, but that's half the fun of it, making it smarter.

avatar image
Best Answer

Answer by Ricardo · Feb 27, 2010 at 08:22 AM

T$$anonymous$$s steering behavior and others are implemented in UnitySteer, w$$anonymous$$ch is a port of Craig Reynolds' OpenSteer (the reference on autonomous steering that MotionReactor linked to is Reynolds' as well).

The function you'd be interested on is steerToAvoidObstacles, or maybe steerToAvoidNeighbors.

Once you begin getting into the fnier points of steering, such as a group of ve$$anonymous$$cles attempting to behave reasonably when encountering each other, you might want to look at Mikko Mononen's Project Cane, as well the other articles he has posted on $$anonymous$$s site on the topic of velocity obstacles.

To close, I don't actually recommend you use raycasting for every aspect of navigation. For static areas like walls, a navigation mesh and AngryAnt's Path is a better option.

Comment
sacredgeometry

People who like this

1 Show 3 · 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 Motionreactor · Feb 27, 2010 at 02:49 PM 0
Share

Nice. I didn't know UnitySteer existed, Awesome! Thanks.

avatar image sacredgeometry · Feb 28, 2010 at 01:07 AM 0
Share

I will read, download and explore those libraries as soon as i have the time, and as always any help is appreciated so thank you.

Peace

Brian

avatar image Karsnen_2 · Aug 27, 2012 at 05:20 PM 0
Share

Those links are outdated!! can you guys give a new link?

avatar image

Answer by bruceb · Feb 27, 2010 at 05:58 PM

Having done a "bit" of t$$anonymous$$s stuff over the years, I agree with Ricardo's assessment cautioning you against extensive use of ray casting for navigation. The corner problem that you are encountering is a perfect example where ray casting, and more generally, purely reactive steering becomes problematic. For example, in corners, as you are turning away from the corner you typically wind up moving closer to one side of the corner or the other. A purely reactive solution (e.g., turn in the direction of most space) will cause you to move back into the corner and cause the oscillation you are seeing.

As Ricardo mentions, Mikko Mononen's website has a superb set of links to the latest papers in the field, most of w$$anonymous$$ch are based on a concept known as velocity objects (and variants such as reciprocal velocity objects). The big idea of velocity objects is that, based on the relative motion of 2 objects, it divides velocity space into those velocities (speed and direction) that are collision-free over some time horizon, and those velocities that will result in a collision over that time horizon. When tied to a planner that provides, in effect, a preferred velocity (namely one that will get you to the goal position), you can arrive at the closest velocity to your preferred velocity that will still be collision-free over the time horizon. At the end of the day you need a robust local steering (collision-avoidance) system as well as a nav. planner of some sort.

Comment
sacredgeometry

People who like this

1 Show 0 · 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

1 Person is following this question.

avatar image

Related Questions

Collider Vision AI question. Solved! 0 Answers

Help with a chasing script 2 Answers

help with AI avoidance script 0 Answers

Problems with raycast obstacle avoidance 1 Answer

Can I make Ai using only a focus script and a bullet script? 1 Answer


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