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 /
avatar image
1
Question by Shawn Miller · Apr 20, 2012 at 03:14 PM · c#rotationdirection

How do I find this direction vector?

I'm trying to find a vector which is facing the direction of the camera and is parallel to the x-axis so that I can apply an even force regardless of the x-rotation of the camera. What I have below works but I feel like it's sloppy and there's another way. Is there and if so, what is it?

 float camAngle;
 Vector3 moveDirection;

 camAngle = Camera.main.transform.eulerAngles.x;
 Camera.main.transform.eulerAngles = new Vector3(0, Camera.main.transform.eulerAngles.y, Camera.main.transform.eulerAngles.z);
 moveDirection = Camera.main.transform.TransformDirection(Vector3.forward);
 Camera.main.transform.eulerAngles = new Vector3(camAngle, Camera.main.transform.eulerAngles.y, Camera.main.transform.eulerAngles.z);
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

2 Replies

· Add your reply
  • Sort: 
avatar image
5
Best Answer

Answer by Kryptos · Apr 20, 2012 at 03:19 PM

Try this:

 // Get the direction of the camera
 moveDirection = Camera.main.transform.forward;
 // Make this vector planar (xz-plane)
 moveDirection.y = 0.0f;
 // Normalize to get a unit vector (optional)
 Vector3.Normalize(moveDirection);
Comment
Add comment · Show 4 · 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 Shawn Miller · Apr 20, 2012 at 03:25 PM 0
Share

Wow, I don't know why I didn't think of that at all...

avatar image ForceMagic · Jan 11, 2016 at 08:32 PM 0
Share

Note: tranform.forward seems to be already normalized, at least in Unity 5.2 it is.

avatar image tanoshimi ForceMagic · Jan 11, 2016 at 08:46 PM 1
Share

transform.forward has always been a unit vector. The normalisation is required to maintain a unit vector after having set y=0.

avatar image ForceMagic tanoshimi · Jan 11, 2016 at 08:56 PM 0
Share

Oh! you're right, my mistake!

avatar image
0

Answer by ExTheSea · Apr 20, 2012 at 03:20 PM

When you have a 3-dimensional vector whith x, y and z and you delete the y coordinate you get a 2dimensional vector which is not pointing up or down.

 newVector=Vector3(oldVector.x, 0, oldVector.z);

hope this works if it isn't the y try the same thing with x and z.

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

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

6 People are following this question.

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

Related Questions

Flip over an object (smooth transition) 3 Answers

Dynamically change Vector3.up/right?,How to make a cylinder chase mechanic? 2 Answers

Instantiated objects always facing the center 2 Answers

How to snap a model to the direction being moved in, without also rotating the character controller 3 Answers

Multiple Cars not working 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