- Home /
How to make object move away from nearby objects
Making a tank battle game where there are 3 tanks per side on a 2D field and the player controls one tank while the others are controlled by AI. I'm just trying to make a simple AI where the tank detects if its near other tanks and moves away from them but am at a bit of a loss as to how to implement this. I've been toying with using Physics.OverlapSphere but I don't how to tell in which the direction the object is when I've got the colliders. Any ideas?
Sorry about that, I'm fairly new to this. This was helpful.
Answer by Fattie · Sep 29, 2012 at 04:52 PM
say you are computer tank AA
say one of the other tanks is called TT
directionToTT = TT.position - AA.position; directionToTT = directionToTT.normalized;
now, you want to move in the other direction
directionIWantToMove = -1 * directionToTTl
So, you should move in that direction.
NB you do not have to use raycasting, spheres etc. Because, you simply know all the things in the scene. Just use those gameObjects.
Your answer
![](https://koobas.hobune.stream/wayback/20220613081641im_/https://answers.unity.com/themes/thub/images/avi.jpg)