[Solved]LookAt vs LookRotation


Recommended Posts


I am trying to replicate a very small detail from my unity project to Unigine and it's causing a lot of trouble even tho I have seen the Third Person Demo Code it still is pretty unorthodox.

Unity C# Script:

            Vector3 targetDirection = (Target.position - this.transform.position).normalized;
            Quaternion targetRotation = Quaternion.LookRotation(targetDirection, Vector3.up);
            transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, Time.deltaTime * RotationSpeed);

This is a small script for turrents they rotate towards target.

Unigine C# Script:

        vec3 targetdir = TargetNode.WorldPosition - node.WorldPosition;
		quat targetRot = new quat(MathLib.LookAt(vec3.ZERO, targetdir, vec.UP, MathLib.AXIS.Y));

the results are not similar.


I Did manage to get the desire result by a work around:

        vec3 targetdir = TargetNode.WorldPosition - node.WorldPosition;
        node.SetWorldDirection(targetdir, vec3.UP, MathLib.AXIS.Y);

but i still want to Limit Rotations i.e. so the turrent is not able to shoot if the target is right at top of it, so i guess i would need LookAt() nevertheless.

Any clue what i might be doing wrong? 

Edited by WarDuck
Link to post


In this case, you can use the WorldLookAt function of the node (https://developer.unigine.com/en/docs/future/api/library/nodes/class.node?rlang=cs#worldLookAt_Vec3_void). The LookAt function returns the viewing matrix, and it must be inverted to use it to get the desired rotation. Instead of LookAt, you can use the SetTo function (https://developer.unigine.com/en/docs/future/api/library/math/math.matrix?rlang=cs&words=lookat#setTo_constvec3n_constvec3n_constvec3n_int).

Link to post

Sorry for the follow up @karpych11 but There seems to be a tiny Issue.

My Code is working as expected for the base of turret but the Pitch of Barrel is messed up at close distance.

Unigine C# script:

// pitch
vec3 direction = TargetNode.WorldPosition - BarrelOfTurret.WorldPosition;

quat rot = new quat(MathLib.SetTo(vec3.ZERO, direction, BarrelOfTurret.GetDirection(MathLib.AXIS.Z), MathLib.AXIS.Y));
vec3 upRotation = MathLib.DecomposeRotationXYZ(rot.Mat3);
vec3 baseRotation = MathLib.DecomposeRotationXYZ(node.GetWorldRotation().Mat3);

// Zero Out Roll And set only pitch and Yaw
BarrelOfTurret.SetWorldRotation(new quat(MathLib.ComposeRotationXYZ(new vec3(upRotation.x, 0, baseRotation.z))));


Unity C# script:

Vector3 direction = Target.position - BarrelOfTurret.transform.position;
Quaternion rot = Quaternion.LookRotation(direction, BarrelOfTurret.transform.up);
Vector3 upRotation = rot.eulerAngles;

BarrelOfTurret.transform.rotation = Quaternion.Euler(upRotation.x, transform.rotation.eulerAngles.y, 0);

I have attached the Node/Game Object Structure Images as well. 


EDIT: I managed to get some result by simplifying the code It works as expected for now.

quat rotationOfTurrent =  new quat( MathLib.SetTo(vec3.ZERO, direction, vec3.UP, MathLib.AXIS.Y));
quat currRot;
MathLib.Slerp(out currRot, BarrelOfTurret.GetWorldRotation(), rotationOfTurrent, PitchRotateValue * Game.IFps);



Edited by WarDuck
Link to post
  • WarDuck changed the title to [Solved]LookAt vs LookRotation