Search Unity

Rotate away from multiple objects

Discussion in 'Scripting' started by Shack_Man, Feb 15, 2020.

  1. Shack_Man

    Shack_Man

    Joined:
    Jun 7, 2017
    Posts:
    372
    I'm making a wildlife game where animal A can get chased by any number of animal B, and starts fleeing.

    If I have one chaser (animal B), I just rotate animal A in the opposite direction. How could I solve this for multiple animal B? I basically need to find the direction with the biggest angle difference to all directions pointing at the chasers. Right now I can only think of a dirty solution, which is to test out a bunch of directions and check each angle difference. This might actually be quite good for this case where I want to simulate wildlife, but I'm still interested in the direct solution.

    Thanks!
     
  2. Owen-Reynolds

    Owen-Reynolds

    Joined:
    Feb 15, 2012
    Posts:
    1,998
    Have each chaser produce a "run-away" arrow. Add them up to get the actual flee direction.

    The run-away arrow is directly away from the enemy, the same as you were using. The length is based on how scary they are and how close. That way, when you add them, you'll run mostly away from the scarier/closer monster. It won't work when 2 monsters are exactly opposite (you should run at a right angle), but that will never happen, and it works if they're 179.9 degrees apart.