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 majak · Mar 09, 2015 at 06:10 PM · math

Implementation behind Transform.forward

I wondering what philosophy stands behind Transform.forward setter. In my opinion it should rotate coordinate system by smallest angle needed to make forward vector equal to new value. So implementation should be something like that:

 class Transform
 {
     private Vector3 _forward;
 
     public Vector3 forward
     {
         set
         {
             Vector3 oldForward = _forward;
             Vector3 newForward = value;
 
             Vector3 axis = Vector3.Cross(oldForward, newForward).normalized;
             float angle = Vector3.Angle(oldForward, newForward);
 
             transform.rotation = transform.rotation * Quaternion.AngleAxis(angle, axis);
         }
     }
 }

right? But I noticed, that Transform.forward setter works exactly in the same way as Transform.LookAt(transform.position + transform.forward).

So my question is: How does Transform.forward setter works? What happen with up and right vectors?

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

4 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by majak · Mar 10, 2015 at 02:39 PM

I found something in that post: http://answers.unity3d.com/questions/560471/efficiency-of-transformforward-up-and-right.html (Bunny's answer).

 public Vector3 forward
 {
      get { return this.rotation * Vector3.forward; }
      set { this.rotation = Quaternion.LookRotation(value); }
 }

And now I understand why transform.forward works exactly the same as Transform.LookAt() ;D

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 Owen-Reynolds · Mar 10, 2015 at 03:35 PM 0
Share

...and if you see the pop-up for LookRotation, it has a second slot for the local UP. The way local up defaults to world up in the 1-input version is standard design. People who didn't know what LookAt did would still know it had 2 inputs, but the 2nd one was kind of advanced.

With a setter, there's no good way to supply the second input, or even to say there is a secrete second input. The author is forced to lock it into one particular local up, so it can never work properly.

avatar image maxnorth · Mar 10, 2015 at 04:07 PM 0
Share

I'm surprised that this satisfies you, since it really only shows that it does use the same kind of function without saying why it is better than the method you suggested. Notice that the get for this function returns the rotation multiplied by Vector3.forward. This tells you that the directional vectors (Transform.forward/right/up) are all based on the rotation of the Transform. It makes sense then, that in order to define a new forward for the transform, you have to define a new rotation for it (and then the forward/right/up vectors are all relative to that rotation). They key part here is that it is impossible to define a rotation with only one vector, so there is difficulty in setting the Vector3 transform.forward by assigning a single vector value. The Quaternion.LookRotation method defines a rotation with two vectors, one for the direction it is facing, and one to indicate which way is 'up'. When LookRotation() is called with only one parameter as in this post above, it implicitly passes the world vector Vector3.up as the second parameter. This allows transform.forward to be set by a single Vector3 value (since it assumes the second value needed to define a rotation).

avatar image majak · Mar 10, 2015 at 04:33 PM 0
Share

This answer satisfies me, because I understand why Transform.forward setter is "mathematically not correct". In my pinion "mathematically correct" setter should use Quaternion.FromToRotation() ins$$anonymous$$d of Quaternion.LookRotation(), because Quaternion.FromToRotation() gives you smallest angle needed to rotate forward to reach new direction.

But Quaternion.LookRotation() is more convenient from the game dev point of view, beucase Quaternion.FromToRotation() will give you unwanted roll angle in your coordinate system, and Quaternion.LookRotation() with default up vector will not (right.y will be always 0.0).

avatar image Owen-Reynolds · Mar 11, 2015 at 05:20 AM 1
Share

Another way to look at is it that it's just math, and doesn't know how you're using it.

If you use it every frame, making a small change, then respecting current spin seems natural. But someone else might have a 1-time transform.forward=Vector3.forward; to snap it back to standard facing. They'd be confused if it gave random-see$$anonymous$$g spins. And it can't figure out to work differently each way.

avatar image
0

Answer by Owen-Reynolds · Mar 09, 2015 at 07:55 PM

It's a misunderstanding of what these are. transform.forward is just a simple Vector3. It's a length one direction vector -- your personal blue arrow. It has no right or up vector. It isn't missing. It just can't have one.

LookAt gives a rotation/quaternion. It has an extra part. It aims down a direction, with a local z-spin (the thing which is normally set to UP.) 'LookAt` says "OK, this is a Vector3 direction that has no spin, since directions can't even have spins. I'm going to aim this way and also make-up a spin and add it, to get a Quaternion."

Comment
Add comment · Show 2 · 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 majak · Mar 09, 2015 at 09:42 PM 0
Share

Yes, I know that forward is "just" a simple vector. But please notice, that forward's setter has some logic - when you change forward, then Transform also changes rotation component, with the result that right and up vector also changes.

avatar image Owen-Reynolds · Mar 10, 2015 at 03:18 AM 0
Share

Ah, I see your point. Having a setter for transform.forward is just junk. Should be readOnly. I think it's only there because most people will know not to use it, but it sort of works for people who haven't learned LookAt yet (yes, I've seen people use it that way.)

avatar image
-1

Answer by toddisarockstar · Mar 10, 2015 at 04:10 AM

transform.forward is just an easier way to say it. its a feature that the friendly unity developers gave us that requires a couple less milliseconds time for a cpu to figure if it matters. Transform.LookAt() requests an unnessassary calculation from the game engine and your script waits for a return between TWO points. it may not matter to a lot of projects but if you had a game calculating 100 objects every frame it would matter. think of transform.forward as a shortcut.

you can do really basic stuff with out euler angles but really there is a lot of stuff you cant do without them. so you will eventually need to learn. hopefully before you end up banging your head against the keyboard trying to figure out why you cant get a script to do what you want.

actually, transform.forward is a euler angle. it's an exact abbreviation for Quaderton.Euler(0,0,1)

as you could guess...Quaderton.Euler(0,0,-1) is backwards and so on....

eulers are actually simpler to script and some funtions you need to call rely on them!

Comment
Add comment · Show 2 · 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 majak · Mar 10, 2015 at 11:24 AM 0
Share

Hmm, now I'm totally confused;) What do you mean that transform.forward is a Euler angle? From the user point of view, it is just a vector pointing some direction. And transform.forward IS NOT equal to Quaternion.Euler(0, 0, 1) (I suppose you mean Quaternion, not Quaderton). Quaternion.Euler(0, 0, 1) returns rotation around Z axis by 1 degree

avatar image Owen-Reynolds · Mar 10, 2015 at 03:06 PM 0
Share

The confusion might be that EulerAngles and Directions are both Vector3s. But directions are added to positions: transform.position+transform.forward.

If you're facing right, your forward is (1,0,0) and your EulerAngles are (0,90,any).

$$anonymous$$ost people start already know EulerAngles pretty well. Every knows 90,180,270. The hard part is using quaternions directly, and not even trying to think of EulerAngles.

avatar image
0

Answer by tanoshimi · Mar 09, 2015 at 06:12 PM

It's unlikely you'll get an authoritative answer, because that would require knowledge of internal workings of Unity that the community at large won't have. However, I doubt that it looks anything like your suggested implementation - to my knowledge, all internal rotation calculations are handled via Quaternions, not very Vector3 Euler angles.

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 majak · Mar 09, 2015 at 09:48 PM 0
Share

Thanks for your answer. I don't use Euler angles either. What I was trying to say that I don't understand why Transform.forward works exactly the same way as Transform.LookAt()

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

attribute Percent used 1 Answer

Unity not giving correct solution to equation 1 Answer

How do I calculate the population variance and sample variance? 0 Answers

Player rapidly accelerating instead of stopping 1 Answer

Cap Slerp rotation if above certain angle 0 Answers


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