i have tried using lookout method then flipping it by 180 degress but it gives me the same result as the problem i had before. my turret turns left when i move right and vica verse. To fix this i used my trigonometry skills to get how much it need to turn on the x and y axis.but i still get the same trouble so i the result negetive but this in turn flips my character. here is the code,i have modified it a bit so it wont look as nice as i am tired:(

 Vector3 distance = Trunk.GlobalPosition - TrackedObject.GlobalPosition;
 float rotatx = Mathf.Atan2(distance.Y , distance.Z);
 float rotatey=Mathf.Atan2(distance.X, distance.Z); //doing the opposite pattern
 Vector3 rot=Trunk.Rotation;
 Vector3 newrot =Trunk.Rotation;
 newrot.Y = rotatey;
 newrot.X = rotatx;
Trunk.Rotation = rot.Lerp(newrot, 0.1f);

here is a video to show the problem: Video (dont mind the clear trigonometry problem,i was tired and mixed up pi/2 with 90)


1 Answer 1


This is the solution i came up with:

Vector3 TargetDirection = Trunk.GlobalPosition - TrackedObject.GlobalPosition;
TargetDirection = TargetDirection.Normalized();
Basis temptrunk = Trunk.GlobalBasis;
temptrunk.Z = TargetDirection;
temptrunk.Y = TargetDirection.Cross(temptrunk.X);
Trunk.GlobalBasis = temptrunk;

The reason why it works is becouse the vector from the trunk to the target is the direction the trunk should be pointing. I then normalize it so that i only get the direction. Then i set the z basis[think of the z basis as the face of the object,y basis as the angle in space of the object and the x axis as the side of the object] so it has the same direction. Then i cross my y & x axis as i want my trunk to be scaled properly. This can also be applied on the different basis,it is important to know what you want to face what.

  • 2
    \$\begingroup\$ This answer would be even more helpful to future readers with a little explanation clarifying why or how this code solves the problem. \$\endgroup\$
    – DMGregory
    Commented Mar 7 at 17:53

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .