Rotate GameObject on one axis to face another

Hey guys,

Wondering if you could help with something. Im currently making a top down game and I’m having trouble getting a game object to slowly rotate on one axis to face another game object.

Here is the code i’ve tried. Someone on the answers site suggested it.

var lookDirection = target.position - turret.transform.position;
lookDirection.z = 0;
			
Quaternion targetRotation = Quaternion.LookRotation(lookDirection, Vector3.up);
turret.transform.rotation = Quaternion.Slerp(turret.transform.rotation, targetRotation, Time.deltaTime * 2.0f);

The problem is this rotates the sprite so that the z+ of the sprite is facing the player.

The way I have the 2d game setup is

y+
^
|
o - >x+

Camera faces the z+ direction.

So z+ direction on the game objects is the bottom side of the sprite. The above script make the bottom side of the sprite face the character.

All I want is the game objects(turret)'s y+ to rotate and everything else stay the same.

I also tried setting the x and z to 0 on the lookDirection with no success. All it does is sort of flip the turret as the player moves by.

Also the game object I want to rotate is inside another game object. Will this cause problems? I need to keep it inside its current object.

Any thoughts? Thanks

If it’s only turning on one axis and the game is all on a single plane you could simplify things by just using the z rotation.

If you can calculate the angle between the turret and the player using trigonometry you should be able to set the z rotation using transform.eularAngle.

EG - gameObject.transform.eularAngle(gameObject.transform.eularAngle.x,gameObject.transform.eularAngle.y, zAngle)

You may want to look at some of the angle functions in Mathf like LerpAngle and MoveTowardsAngle to help with rotating past 360 degrees.

May be wrong about it being the z angle (and there may be quicker ways)

Thanks for the tip RicheyMB, I’ll give it a shot tonight and let you know how it goes.

Thanks for the tip. Worked out a treat.

Used this for the rotation

var dx = turret.transform.position.x - target.transform.position.x;
var dy = turret.transform.position.y - target.transform.position.y;
var radians = Mathf.Atan2(dy,dx);
var rotateZ = radians * 180 / Mathf.PI - 90;

turret.transform.eulerAngles = new Vector3(turret.transform.rotation.x, turret.transform.rotation.y, rotateZ);

I minus 90 just because I drew my sprite turret at the wrong angle. Just need to convert it to use the LerpAngle and it should all be sweet.

Thanks again.

I’m having a little trouble with LerpAngle,

I can’t get it to rotate over a time limit. I tried Time.deltaTime * 2.0f but it won’t rotate. I checked the docs and it shows in the example its using Time.time. The rotation works but it happens instantly?

Here’s the code,

var dx = turret.transform.position.x - target.transform.position.x;
var dy = turret.transform.position.y - target.transform.position.y;
var radians = Mathf.Atan2(dy,dx);
var angle = radians * 180 / Mathf.PI - 90;

var rotateZ = Mathf.LerpAngle(turret.transform.rotation.z, angle, Time.time);

turret.transform.eulerAngles = new Vector3(turret.transform.rotation.x, turret.transform.rotation.y, rotateZ);

I can’t seem to get it to rotate over time using the LerpAngle?

Scrap that, i worked out what was wrong. I thought I should be multiplying by 2.0 as in seconds but its treated more like speed so it was rotating very slow.

All working now. Thanks again.

Nice job @anon_93753554 . Thanks!

I would add a suggestion of not to assign EulerANgles to turret but rather use Quaternion.Euler() to turret.rotation.