- Home /
Determining rotation for collision avoidance?
Hi i was wondering what the best way to determine which way a character should rotate when it its ray returns a hit.
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
// C# code version of JS provided by Motionreactor below RaycastHit hit; Ray ray = Ray(gameObject.transform.position, Vector3.forward);
if (Physics.Raycast(ray, hit, 10))
{
Debug.Log(hit.distance);
Debug.DrawLine(ray.origin, hit.point);
}
Answer by Motionreactor · Feb 26, 2010 at 04:37 PM
You can cast a ray in a specific direction like this:
var hit : RaycastHit;
var ray : Ray = Ray(gameObject.transform.position, Vector3.forward);
if(Physics.Raycast(ray, hit, 10))
{
Debug.Log(hit.distance);
Debug.DrawLine(ray.origin, hit.point);
}
Vector3.forward could be any Vector direction you wish.
Sorry my fault, i've got snow blindness from staring at too much code.
:) heh we just went through deleting comments at the same time... so yeh, no worries
Oh, btw.. I remembered this awesome reference on autonomous steering: http://www.red3d.com/cwr/steer/
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.
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.
Answer by Ricardo · Feb 27, 2010 at 08:22 AM
This steering behavior and others are implemented in UnitySteer, which 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 vehicles 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 his 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.
Nice. I didn't know UnitySteer existed, Awesome! Thanks.
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
$$anonymous$$
Those links are outdated!! can you guys give a new link?
Answer by bruceb · Feb 27, 2010 at 05:58 PM
Having done a "bit" of this 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 which 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.
Your answer
Follow this Question
Related Questions
Collider Vision AI question. Solved! 0 Answers
Help with a chasing script 2 Answers
help with AI avoidance script 0 Answers
AI behaviour in 3D game, general help needed 2 Answers
Can I make Ai using only a focus script and a bullet script? 1 Answer