- Home /
Avoidance Behaviour
Hello. I would like some general tips or perhaps overall concepts to avoidance behaviour. I currently have come up with a somewhat working solution involving mirroring what I want to advoid, but if you couldn't guess, that only half works as mirrors are like that. I've heard about steering behaviour but it sounds more complicated then I want, but if there isn't any better way I suppose it could work. Path finding is also related to avoidance behaviour but with my paths changing so often I'm not sure if that is the best method.
Currently for some quick advoidance behaviour I have been using this code snippet I came up with.
transform.position = Vector3.MoveTowards(transform.position, -myPlayer.player.position, moveSpeed * Time.deltaTime);
I know that getting by normalizing the transform.position and player position I can tell if the player is moving closer to me as the normalized variable remains the same... But I'm not sure what to do with that, I've never taken any decent math classes....
direction = transform.position - myPlayer.player.position; direction.Normalize();
if I could somehow check if I'm getting the same result from normalize constently and then reverse my direction, that would probably solve my whole issue. Maybe...
Any advice or perhaps websites that go into detail on this sort of thing? This avoidance behaviour will be used on a large group or herd of objects to advoid other herds and enemies.
Heads up, this kind of open-ended question belongs in the scripting forum.
Avoidance behaviors are a sub-category of steering behaviors. Definitely a little math for you, but luckily, the computer does the lion's share of the work. ;)
There are many approaches to setting up avoidance mechanics, each with pros and cons. The fundamentals are simple enough. Steering around static obstacles is pretty straightforward - steering around other actors gets... interesting. No cookie-cutter solutions, I'm afraid, unless you can find a good steering library. (I don't like those I've found).
This is the quintessential resource for simple steering behaviors. Helped me immeasurably when I was where you're at: http://www.red3d.com/cwr/steer/
Thank you, I would have marked this as an answer if you posted it as such. It looks like if I pick apart UnitySteer and this I might get the behaviour I'm looking for. I'm trying to have a shepherd steer a flock of sheep away from enemies and such. It's a complicated AI project but so far it kinda works...
Answer by soft_sound · Feb 05, 2015 at 04:55 PM
Actually, I got my code to work. Steering Behaviours& Avoidance
I used this as my reference and converted it over to C#. It was really simple like I thought it would be. I was quite close!
public Vector3 velocity;
public Vector3 desired_velocity;
public Vector2 d_velocity;
public Vector2 steering;
public Vector3 max_velocity;
.....
if (flee)
{
if (Vector3.Distance(transform.position, player.position) < 5)
{
desired_velocity = Vector3.Normalize(transform.position - player.position);
desired_velocity = Vector3.Scale(desired_velocity, max_velocity);
d_velocity = new Vector2(desired_velocity.x, desired_velocity.y);
steering = d_velocity - transform.rigidbody2D.velocity;
transform.rigidbody2D.AddForce(steering);
}
}
Anyway hope this helps anyone else!
Answer by DanSuperGP · Feb 04, 2015 at 11:29 PM
" I've heard about steering behaviour but it sounds more complicated then I want, but if there isn't any better way I suppose it could work. "
Steering behaviors are about the least complicated of navigation systems. If you don't want to deal with implementing them yourself you could just use UnitySteer
Navigation systems aren't all complicated, simple node based paths and wandering systems are pretty simple, just a few lines of code. Following objects can be simple within a certain context. It seems though Unitysteer has some examples in the advanced section I can hopefully use so I think I'll pick those apart and see what happens. Thank you.
Your answer
Follow this Question
Related Questions
What easiest way to emulate local avoidance? 1 Answer
How to combine NavMesh Agent with Character Controller 0 Answers
How do i get the IsPathPossible() function to ignore some nodes using Astar Pathfinding Project 0 Answers
How do i get the IsPathPossible() function to ignore some nodes using Astar Pathfinding Project 0 Answers
Attacking units surround attacked building properly? (New solution, or fix to Nav Mesh?) 0 Answers