Turret targeting script does not rotate like it should

Hello everyone,

I am running into a little issue with my turret targeting / aiming script.

Here it is :

rotate = Quaternion.LookRotation(target.transform.position - turret_RotY.transform.position) ;
turret_RotY.transform.rotation = Quaternion.RotateTowards(turret_RotY.transform.rotation, rotate, rotSpeed) ;
turret_RotY.transform.localEulerAngles = new Vector3(0,turret_RotY.transform.localEulerAngles.y,0);
          
rotate = Quaternion.LookRotation(target.transform.position - turret_RotX.transform.position) ;
turret_RotX.transform.rotation = Quaternion.RotateTowards(turret_RotX.transform.rotation, rotate, rotSpeed) ;
turret_RotX.transform.localEulerAngles = new Vector3(turret_RotX.transform.localEulerAngles.x,0,0);

It is pretty simple and it works just fine, when the turret is on the ground.

My issue is when i build the turret on a wall, so at a 90 Deg angle. It does not aim properly towards the target, it keeps a little offset, and i am really not sure why…

Any help would be appreciated !

Hi Feydwin!

I’m not sure what exactly you mean but I see something that might be an issue.:
turret_RotY.transform.rotation = Quaternion.RotateTowards(turret_RotY.transform.rotation, rotate, rotSpeed) ;

It seems to me as this rotation change is made in recurent manner. In this case you will get slowing down at the end of aming. Your turret is targeting closer and closer to target but never perfect.

Other thing that comes to my mind is “gimbal lock” - but it’s just a loose association.

Hi Laufer, thanks for your answer.

You are right, the turret does slow down a little at the end. But to be honest, i like that behavior…

But what is really bothering me, and i am yet to find a way to fix that is the rotation of the turret when the turret is build on the wall.

Basically. my game goes like this : i click on the ground, it spawns w/e prefab i selected previously, at the location of the click. But what i had in mind when designing the game ( which is an FPS tower defense ), was to be able to build ALSO on walls and ceilings. So i managed to get the transform.rotation onto w/e the prefab is built and spawn it properly.
The big issue i have is the following : On the ground, the turret aims properly. On walls ( So spawned with a 90 Deg Angle ), the aim is completly off target. Basically, if the turret is anything else but 0.0.0 in rotation, the aim is off…

I have tried for a few days now, trying to dive into Quaternions, and such… but to be honest, i am either a big dumbass, or their is a glitch. I tend to think it s the first option… Cause i managed to make it kind of work with the “LookAT” function. But when i tried to transfer rotation to Quaternion, in order to be able to use Slerp easily, it gets all screwed up again. I tried so much that i don t recall everything, but i am out of things to try now…

Anyways, i think i am going to re-design the game… i didn t find one tutorial that explains how to make things rotate properly when not at a 0.0.0 rotation originally…

If you have any solutions, or could point me to what i have to learn in order to fix this, that would be greatly appreciated.

Note the documentation for LookRotation:

There’s a second optional parameter that defaults to Vector3.up. This is the ‘up’ vector to calculate the look forward from.

By default it assumes the look rotation is something that is standing on the ground, so up would be… Vector3.up.

But if you’re on the wall, up might be something different (it’s the normal of the surface you’re on).

It needs this information, otherwise it doesn’t know at what angle around the facing direction you want to be (imagine if I said look straight… does it mean look straight while standing on your head, or standing on your feet? You ASSUME on your feet, because that’s what we do 99% of the time.)

1 Like

OMG…
ok like i said… the first option was the answer ( big dumbass ).
THANKS A THOUSAND TIMES !