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
0
Question by OldLadyNevermore · Jul 31, 2013 at 12:32 AM · cameravector3

Need to flatten a Vector3

I have some code that is meant to move the object in the direction the camera is facing, but only along a flat plane. In other words if the camera is pointed North, but slightly down, I want to go North, but not down. If the camera is pointed East, I want to go East, but not down etc.

Here is the code I have am using now:


transform.position += new Vector3(cameraTransform.forward.x, 0, cameraTransform.forward.z) * Input.GetAxis("LeftStickVertical") * speed * Time.deltaTime;

It works fine until the camera is at a very high angle (close to looking straight down): I am pretty sure think this is because at this angle 'cameraTransform.forward.z' approaches zero.

So how do I build a vector for the current direction the camera is pointing, ignoring up and down. I guess another way to ask this is "how do I move the object in the same direction the camera is pointing, Y axis only?"

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
4
Best Answer

Answer by robertbu · Jul 31, 2013 at 12:37 AM

If you are pointed straight down, then there is no direction, but you can solve any problems other than straight down like this:

 Vector3 v3 = cameraTransform.forward;
 v3.y = 0;
 v3.Normalize();
 if (v3 != Vector3.zero) {
     transform.position += v3 * Input.GetAxis("LeftStickVertical") * speed * Time.deltaTime;
 }

With your code, the new flattened Vector3 was getting very short when the angle is steep, because the major component that was contributing to the unit one length of the orginal forward vector was removed. By normalizing the vector, you have a vector of constant unit one length for your movement.

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 OldLadyNevermore · Jul 31, 2013 at 01:20 AM 0
Share

That worked perfectly. Thanks!

avatar image
3

Answer by monotoan · Jan 29, 2018 at 10:08 PM

Updated answer to reflect current scripting API:

Unity's "Vector3" has a built in function that can flatten any vector onto any plane. You pass in the vector you want to flatten as the first parameter, and the normal of the plane you want to flatten it onto as the second -- the normal of a plane being the normalized vector that points straight up from the plane's surface.

The normal of the Horizontal (XZ) plane in world space is Vector3.up. So, to flatten a vector on to this plane, you would do this:

 Vector3 flattenedVector = Vector3.ProjectOnPlane(vectorToFlatten, Vector3.up);

Or, if you wanted to project a vector onto, say, a vertical wall aligned to the world XY plane:

 Vector3 flattenedVector = Vector3.ProjectOnPlane(vectorToFlatten, Vector3.forward);
Comment
Add comment · Show 3 · 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 $$anonymous$$ · Dec 15, 2019 at 05:40 PM 1
Share

This does not solve the original problem since if the camera's forward vector is passed as the vectorToFlatten, you will get varying lengths for the flattenedVector based on the pitch of the camera. The vector still needs to be normalized. This would fix it: Vector3.ProjectOnPlane(vectorToFlatten, Vector3.up).normalized

but then it would be the same as the originally accepted answer.

avatar image Bunny83 $$anonymous$$ · Dec 15, 2019 at 07:17 PM 1
Share

Right, normalization is still necessary. Though if you only need the x-z-plane the usage of PorjectOnPlane is actually overkill because the result would be the same as if you just set the y component to 0.

avatar image $$anonymous$$ Bunny83 · Dec 15, 2019 at 09:06 PM 0
Share

Yeah it's definitely overkill.

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

17 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

Related Questions

How to detect VR camera movement? 0 Answers

Why is my Camera rotation tilted after parenting it to head? 1 Answer

Ray through camera 1 Answer

Problem with the rotation of camera in third person 1 Answer

C# 2d Spawn Randomly along Camera Borders 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