Home > database >  How to get an object's direction relative to the camera?
How to get an object's direction relative to the camera?

Time:05-02

I only know how to do it when the camera's Y rotation is set to 0.

float Angle = transform.rotation.eulerAngles.y;
             if (Angle > 315 || Angle < 45)
                 dosomething();
             else if (Angle < 225)
                 dosomething();
             else if (Angle < 135)
                 dosomething();
             else
                 dosomething();

However, this won't work when the camera rotates, since the "forward angle" is not 0. I seriously don't know how to the angle calculation when the angle goes over 360.

CodePudding user response:

If want to know the difference between an object's facing direction and the camera's facing direction: you can try this.

 float angle = Vector3.Angle(camera.main.transform.forward, object.transform.forward);
  • Related