- Home /
rotate two objects around each other and then keep them moving
Ok, so, I'm very new to programming and I've basically been learning on the fly. I've come across an issue where I've written a script wherein an object wanders randomly until it reaches a certain distance and if it exceeds that distance, returns to it's starting point. I've also written a function where all the other similarly tagged objects are put into a list and a distance is checked between them. If the distance becomes too close I wanted them to rotate around each other until they reached their previous movement direction and keep moving. The problem is, before I can even worry about the object reaching it's previous movement direction, the objects spin into each other until they are fiercely rotating together, kind of like planets or a black hole. How can I keep this from happening?
public void AvoidObstacles () {
float enemydistance = float.MaxValue;
if (companions == null) {
return;
}
foreach (GameObject enemy in companions) {
enemydistance = Vector3.Distance(enemy.transform.position, transform.position);
Vector3 dir = (enemy.transform.position - transform.position).normalized;
if (enemydistance <= 1f) {
curRotation = myTransform.TransformDirection (Vector3.forward);
Ray ray = new Ray (curPosition, curRotation);
RaycastHit hitInfo;
if(Physics.Raycast(ray, out hitInfo, 1f)){
Vector3 rotatePoint = Vector3.Lerp(myTransform.position, enemy.transform.position, 0.5f);
transform.RotateAround(rotatePoint, Vector3.up, rotateSpeed * Time.deltaTime);
enemy.transform.RotateAround(rotatePoint, Vector3.up, rotateSpeed * Time.deltaTime);
}
}
}
}
Your answer