Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
This question was closed Feb 27, 2014 at 02:56 AM by DiligentGear for the following reason:

Problem is not reproducible or outdated

avatar image
0
Question by DiligentGear · Jan 04, 2014 at 06:36 PM · cameradirectionthird-personfacing

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?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

3 Replies

  • Sort: 
avatar image
1

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);
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by NightmarexGR · Jan 04, 2014 at 10:49 PM

Blockquote

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.
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image DiligentGear · Jan 04, 2014 at 11:21 PM 0
Share

alt text

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
 }
sector.png (15.6 kB)

Follow this Question

Answers Answers and Comments

21 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Gun clipping through walls in third person shooter 1 Answer

Change characters direction using camera view 0 Answers

How can I apply a force to an object (eg, a rolling ball) in the direction that the camera is facing? 2 Answers

Move character in direction its facing 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges