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.

`

" 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

http://arges-systems.com/blog/category/unitysteer/

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!