Hello everyone,
now I have a little prolem I couldn’t fix entirely.
I have a compass. This compass has a needle which will always show into the direction of the nearest exit. The problem here is, that the compass needle rotates in all 3 dimension. I want that the needle only rotates in the compass local plane, so parallel to it’s local surface. My compass needle should not stick out of the compass, that’s weird.
I have the following method for this:
private void RotateTowardsNearestExit() { Vector3 dir = GetNearestExitPosition() - pointer.transform.position; Vector3 pointerDir = Vector3.ProjectOnPlane(dir, transform.up); pointer.transform.forward = pointerDir; }
First I get the direction, where the pointer (compass needle) has to look at. Then I used the method “ProjectOnPlane” so that this direction will be set on the local surface of the compass. I used therefore the transform.up to get the normal vector of the compass, which is needed for “ProjectOnPlane”. At last I say that the pointers forward direction, in which it shows, is this pointerDir.
It sounds perfectly, but it does not work properly. If the compass is not parallel to the X-Z world plane, the needle still tries to rotate out of the compass. Here a picture: