Hey there. I got a big problem.

I need to rotate a Turret around 2 Axis.

Those 2 Axis build up a Volume where the Turret can Aim to.

For Example:

Around Y Axis → min = -50° max = 170°

Around X Axis → min = 0° max = 180°

This would make the Turret forbid to enter a small part of a sphere.

The turret gets a Target (which can move) and shall automatically aim.

I tried it with Quaternions but modifying the eulerAngles to only rotate in one direction

then check the second angle and its limitations and then rotate the next axis.

This ends up in a mess. They interfere with one another somehow.

Now I have a question. Does Unity have a build in pattern for that? Or is there any easy way?

I came to think of a Collider with the Shape of the Sphere with only the allowed parts present(

like a Sphere which has some cut-out parts) and check if the gun will rotate outside this Sphere on the next Iteration.

Anywas I am desperated as I tried so many things. Which all were useless, buggy or jittering.

Is there any way how to clamp such rotations?

What I noticed also is: If I rotate X around 90° and try to rotate Y it turns around its local Z-Axis.

I am so confused which variable is in which Coordinate-Space

Ah I think I got it.

What I did before:

First I calculated a dX between Gun and Target in Guns Local Space

Then I calculated a dY beteen Gun and Target in Parents Local Space

This Deltas are to determine the Direction of the Angle.

The Angles itselves is calculalated from Vectors2.

Angle Around Y Axis is calculated between:

(GunToTarget.x , GunToTarget,z) and (OriginalGunFwd.x, OriginalGunFwd.z)

This is the Angle between the Original Forward-Vector and the Target (In Gun Local-Space).

As it is in Gun-Local Space, The Fwd-Vector is the acual Fwd-Vector

Angle Around X Axis is calculated between:

(relTarget.y, relTarget.z) and (OriginalGunFwd.x, OriginalGunFwd.z)

Here the Fwd-Vector is the Vector as it was at t=0

as this is calculated in Parents-Space

Multiplied by the Deltas (which is modded to be either 1,-1 or 0) it gives the angle.

The X-Angle should be applied directly (localEulerAngle.x = XAngle)

The Y-Angle is an incremental so it should be added to localEulerAngle.y

BUT THIS WAS WRONG!!

I experimented around and it seem to b like that:

The X Rotation DEPENDS on the Y-Rotation!

I treated them as individuals BUT the order is important.

So I switched dx and dy - Calculus as well as XAngle and YAngle and applied them accordingly.

It works.

Now I need to get a clamping.