- Home /
Duplicate Question
local avoidance question
I know this question has been beaten to death, but I am trying to write my own kind of local avoidance code. I saw another post where someone reccomended finding the distance between 2 objects and then having a vector push the unit in the opposite direction. I tried this method using the code below as a test:
float distance = Vector3.Distance (GameObject.Find("Unit1").transform.position, GameObject.Find("Unit2").transform.position);
if (distance < 2) {
Vector3 direction = GameObject.Find("Unit1").transform.position - GameObject.Find("Unit2").transform.position;
transform.Translate(-direction.normalized *Time.deltaTime);
}
It worked except that the units seem to push each other into the floor, and they are still not exactly repelling each other in the correct way each time. Is there a way I can freeze the Y componenet of the vector? Also I tried the AddForce function but this didnt work either. Anyone know of a way? Thanks,
you don't even explain what sort of game you're making (3d, 2d, chartacter conrtollers, balls with physics, whatever). You can easily freeze physics in unity, search here for a million references to the simple freeze button sin the inspector rigibbody
Follow this Question
Related Questions
Reposition to minimum distance from target 2 Answers
Finding Distances With Vector3 1 Answer
if close to one of a kind? 2 Answers
Keep camera at certain distance from character when rotating 3 Answers
charachters stand next to each other 0 Answers