- Home /
 
Problem is not reproducible or outdated
How to get an object's direction relative to the camera?
I only know how to do it when the camera's Y rotation is set to 0.
This's what I did:
         void GetDirection () {
             float headingAngle = transform.rotation.eulerAngles.y;
             if (headingAngle > 315 || headingAngle < 45)
                 anim.SetBool("GoForward",true);
             else if (headingAngle < 225)
                 anim.SetBool("GoLeft",true);
             else if (headingAngle < 135)
                 anim.SetBool("GoBackward",true);
             else
                 anim.SetBool("GoRight",true);
         }
 
 
               However this won't work when 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.
EDIT: For example, how can I tell whether the player is heading north/south/west/east?
Answer by tanoshimi · Jan 04, 2014 at 11:24 PM
If you simply want to know the difference between an object's facing direction and the camera's facing direction:
 float angle = Vector3.Angle(camera.main.transform.forward, object.transform.forward);
 
              Answer by ShadyD · Jan 04, 2014 at 07:28 PM
Not sure what you are trying to achieve here but maybe take a look at the object's localRotation
Answer by NightmarexGR · Jan 04, 2014 at 10:49 PM
I seriously don't know how to the angle calculation when the angle goes over 360. If your code work fine below 360 try saying something like this: if (MyAngle >=360) { MyAngle -= 360; } I dont know if this is what you are after but if it's not try explaining your situation a little bit more and give us more information about what is that u need.We can't guess on our own.Blockquote

I'm trying to find a generic method to deter$$anonymous$$e whether an angle is inside a certain sector as shown above.
I'm trying my best to do some explanations:
 void GetDirection () {
     float headingAngle = transform.rotation.eulerAngles.y;
     // Facing direction of character
     float cameraAngle = cameraTransform.rotation.eulerAngles.y;
     // Facing direction of camera
    
     if (headingAngle > ClampAngle(cameraAngle + 315) || headingAngle < ClampAngle(cameraAngle + 45))
     // This "or" logic simple won't work
     // For example, the current cameraAngle is 270.
     // Then the forward range is (225,315), and left range is (135,225].
     // In other word, logic of deter$$anonymous$$e left range is 135 < headingAngle < 225
     // So if heading angle is 215, the relative direction should be left, right? Nope.
     // Because the forward logic is (headingAngle > 225) || (headingAngle < 315)!
     // Which always gives "forward"...
     
     // What about "and" ?
     if (headingAngle > ClampAngle(cameraAngle + 315) && headingAngle < ClampAngle(cameraAngle + 45))
     // Try to do this example:
     // Camera angle is 0, and heading angle is 350, which should be forward.
     // In other word, the range is greater than 315 AND lower than 45, which makes no sense
 }