- Home /
Question about specific way of rotation
I'm working on a RTS game where units can move towards eachother and will fight when they touch. These units can rotate independent of eachother which leads to some strange formations. What I'm looking for is a way to rotate the units, after they hit, so they will be either parallel or perpendicular to eachother. Look at the image below to see what I want.
I have tried a few ways to do this but they just don't work out. Anyone has an idea how to start on this problem?
for movement I use:
transform.position = Vector3.MoveTowards (transform.position, target, moveSpeed * Time.deltaTime);
for rotation I use:
transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, rotateSpeed * Time.deltaTime);
Hi !
First of all i'd use Quaternion.Lerp ins$$anonymous$$d of Slerp (it's a personnal choice, i've tried both and i prefer Lerp, i find the rotation more "realistic"), anyway, that's not the matter here ^^
What you can do is, at contact, compute the angle between the forward vectors of the two transforms (so you'll be able to know what is the exact configuration), based on this angle, you can choose what configuration you want to make: parallel or perpendicular. Then you Lerp/Slerp A with respect to B.
Vector3.Angle(Vector3 from, Vector3 to);
http://docs.unity3d.com/ScriptReference/Vector3.Angle.html
Thank you for the answer. How do I get this forward vector? Is it the position they are moving to? if so what if they don't move?
Your answer
![](https://koobas.hobune.stream/wayback/20220613192632im_/https://answers.unity.com/themes/thub/images/avi.jpg)