Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 ryft82 · Oct 07, 2014 at 07:42 PM · movementvector3directionforwardtransform.forward

"Fake" Vector3.forward?

Okay, so I've been trying to find the most efficient and straight-forward way of setting an artificial "forward" movement that functions exactly the same way as Vector3.forward does, but in respect to the GameObject Y rotation. I don't want to account for any other rotation, since this would function exactly like Vector3.forward, but in the relative direction that the object was facing.

So, I'm looking for tips, tricks, pointers or pseudo-code that I can use to piece together a better understanding of how this is accomplished so that I can get the function I need. I don't necessarily want someone to write the code for me, since that defeats the purpose of learning with full understanding most of the time, but even an example would be good if it's just a simple oversight that I'm making.

I know about transform.forward, however this takes all local rotation into account and moves in local forward direction. So either I need a way to define a "fake" Vector3.forward based on direction using Y rotation, or I need a way to remove X and Z rotation from the transform.forward direction so that my movement is always on the same plane, regardless of irrelevant rotations.

Comment
Add comment · Show 1
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 HuskyPanda213 · Oct 07, 2014 at 07:56 PM 0
Share

Vector3 fake = transform.forward; fake.x = 0; fake.z = 0;

Is this what you were looking for?

3 Replies

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

Answer by robertbu · Oct 07, 2014 at 07:59 PM

This should work:

 Vector3 fakeForward = transform.forward;
 fakeForward.y = 0.0f;
 fakeForward.Normalize();
Comment
Add comment · Show 6 · 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 ryft82 · Oct 07, 2014 at 09:03 PM 0
Share

I don't know UnityScript/JavaScript that well, but this looks like it may do about what tanoshimi does below, just in a different way? Any idea which would be more efficient in terms of calculation?

avatar image robertbu · Oct 07, 2014 at 09:23 PM 1
Share

I doubt there is any noticeable performance difference. It will all come down to the pcode and/or machine code produced. The one difference is the 'Normalize()'. If you want to use the resulting vector for movement, you likely want to normalize it.

avatar image ryft82 · Oct 07, 2014 at 09:57 PM 0
Share

Alright, thank you much.

avatar image Pangamini · Oct 08, 2014 at 08:11 AM 0
Share

This sure will cause many troubles, especially when you look straight up

avatar image robertbu · Oct 08, 2014 at 08:36 AM 2
Share

Straight up is an issue, but it is the only one I see. What are the other 'many troubles'. Assu$$anonymous$$g straight up is an issue (often it is not), you can test and use a default value or the previous valid value depending on the need.

 if (transform.forward != Vector3.up)  {
     Vector3 fakeForward = transform.forward;
     fakeForward.y = 0.0f;
     fakeForward.Normalize();
 }
 else {
     // Whatever
 }

Show more comments
avatar image
2

Answer by tanoshimi · Oct 07, 2014 at 08:02 PM

If you want your movement to "always be on the same plane" (and I'm assuming that you mean a flat horizontal plane), don't you simply want to ignore the y component of the transform.forward, i.e.:

 Vector3 fakeForward = new Vector3(transform.forward.x, 0, transform.forward.z);

?

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 ryft82 · Oct 07, 2014 at 08:16 PM 1
Share

I believe what you have here is what I'm looking for. Basically, Vector3.forward moves in a direction in world space in the Z direction without regard to object rotation. There is no X axis movement or Y axis movement, it's simply "forward in world space."

This is what I want to accomplish, but rather than a "strict forward in world space without regard to rotation" I want to define a new "fake forward" that operates the same way, but ins$$anonymous$$d accounts for relative direction (the GameObject Y rotation)...so that no matter whether I'm looking down or up, all movement is happening on a horizontal plane in whatever X/Z ratio translates to a "forward in relative direction" without affecting the Y value at all.

Edit works perfectly fine, but I'm using robertbu's code as it's cleaner and more technically precise in my opinion, and since his answer was provided first, his is the one I will accept as well.

avatar image
0

Answer by EuSuntNoob · Jul 13, 2020 at 05:22 PM

Just an alternative solution for

 Vector3 fakeForward = new Vector3(transform.forward.x, 0, transform.forward.z);

which I believe solves the "looking straight up / down" problem:

 Vector3 fakeForward = Quaternion.Euler(0f, transform.eulerAngles.y, 0f) * Vector3.forward;

Basically I made a rotation similar to transform.rotation but without the "x" component (or "z") and applied it to the Vector3.forward vector. I tested this and it worked even when I looked straight up or down.

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

30 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Problems to project one vector over another 0 Answers

Moving relative to camera direction at constant speed (Velocity problem) 0 Answers

transform.forward doesn't correspond to rotation 1 Answer

iTween Path Editor: ignore Y position in Vector3 1 Answer

How to find the direction to translate an object, to move towards a point, regardless of rotation? 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