1

Am trying to create a 3d compass in unity, but the needle fails to rotate/tilt along with the body of the compass. The needle points accurately towards the north "empty gameobject". But the only problem is it fails to tilt/rotate(up or down) along with the compass body. Below is d script

    public class AmpCompass : ResetObject
{
    public Transform target, compassNeedle;
    public float speed = 1.0f;
    private void Start() {
        
    }

    protected override void ObjectIsGrabbed()
    {        
        base.ObjectIsGrabbed();
    } 

    private void Update() 
    {

        Vector3 needleDir = new Vector3(target.position.x, compassNeedle.position.y, target.position.z);
        compassNeedle.LookAt(needleDir);
    }   
}
Seve
  • 39
  • 6

1 Answers1

0

While I don't really understand your GameObjects' structure, my understanding is that you want to make this compass rotating in a horizontal surface.
However, if you want to manually form a Vector3 and make this transform to look at this coordinate, why do you choose maintain its' Y coordinate? Since I guess you want to maintain the Z coordinate due to the freezing Horizontal Level, which means you should do this in Update() method:

Vector3 needleDir = new Vector3(target.position.x, target.position.y, compassNeedle.position.z);
compassNeedle.LookAt(needleDir);
Ember Xu
  • 344
  • 2
  • 9