0

I have a compass object which rotates and moves along with the camera. Thus the rotation and position of the compass object is adjusted along with the player-controlled camera. Now, having attached a needle to my compass - I would like to have the needle point north.

That is easily done but having the needle point to a northern object. But the needle now doesn't follow the plate (compass object) it is placed on. I would thus like the needle to point accordingly. In other word, rotate singly along the y-axis.

I don't need code - I just need a broad idea of how to do that. I would really appreciate any help :)

Mingan Beyleveld
  • 281
  • 2
  • 5
  • 19

2 Answers2

1

I finally figured it out :D It was much easier than expected... Here is the code if anyone should ever need it:

using UnityEngine;
using System.Collections;

public class Compass : MonoBehaviour 
{
    void Update () 
    {
         transform.localRotation = Quaternion.Euler(0, 360-transform.root.rotation.eulerAngles.y, 0);
    }
}
Fredrik Widerberg
  • 3,068
  • 10
  • 30
  • 42
Mingan Beyleveld
  • 281
  • 2
  • 5
  • 19
  • Mind sharing your hierarchy for this solution (e.g. camera -> compass -> needle)? Is your compass game object pointing north and the needle just following? I am trying to implement your solution with my code but can't figure out the setup you used. Thanks! – Valerie Oct 24 '17 at 20:45
0

I would try to project the vector pointing from compass location to target onto the plane where the needle should lay on. Vector.ProjectOnPlane might be helpful. Something like this should work (not yet tested):

Vector3 dir = compassPos - targetPos;
Vector3 needleDir = Vector3.ProjectOnPlane(dir, compassNormal);
Jay
  • 175
  • 1
  • 6