1

What happens when I set the transform.up value in unity. Specifically could you explain this video in the link below

https://youtu.be/_XdqA3xbP2A

I know it changes what the object is looking at. But, does it look at a position, or how does it work? What happens when I change the direct transform.up? I can’t seem to find a good explanation. Why does the vector in the video make a direction that is facing the mouse, why not use the mousePosition itself instead of subtracting?

Aside from that could you also explain what is going on in the video?

Thanks in advance!

BeastCoder
  • 2,391
  • 3
  • 15
  • 26

2 Answers2

3

Basically, when you move an object, you take its position and rotation as reference, so if you increment +1 on the Y position of an object and this object is rotated, you'll increment on that direction. enter image description here

but with Vector3.up, you use the world space as reference, so enter image description here

i want to give you a full answers here, but i would have to enter with some math, simplifying, the transform.up is the direction which the sprite is pointing, he gets the direction saying this: enter image description here

"mousePosition.y" (GREEN) is the position Y of the mouse, and "mousePosition.x" (RED) is the X position of the mouse, with this two values, you have a coordinate, or a direction (Blue traced line), which unity transforms to quaternions and apply the value to your rotation (this in 2d). you want to use the Vector3 which uses a world space reference, because if you use a local reference, would happen something like this enter image description here

and you would get the wrong direction.

  • But what if you are using rotation, the video is about rotation. Also, it confuses me as to why it would rotate now instead of like you said moving it. Could you maybe explain. Otherwise, AWESOME explanation. – BeastCoder Sep 25 '18 at 23:07
  • Also I am changing the transform.up OF the object, could you please explain? – BeastCoder Sep 25 '18 at 23:14
  • i updated the answers, sorry for the bad explanation, if you search about it, you'll find a proper mathematical answers. –  Sep 26 '18 at 00:56
  • Ok thanks, last question, I promise. So, from you I understand that transform.up uses world space and not local space. Does the way movement work change when I set the transform.up directly, or is it the same and could you please explain a bit more of the third paragraph/picture. But great job, best answer I’ve gotten. I’m going to accept your answer, but I just want to see if there is a little more to my question first. Thanks! – BeastCoder Sep 26 '18 at 10:27
  • I said I understand it is world space but meant local space, sorry! – BeastCoder Sep 26 '18 at 10:34
  • you can see exactly what it does in unity github, here https://github.com/Unity-Technologies/UnityCsReference/blame/master/Runtime/Transform/ScriptBindings/Transform.bindings.cs line 51. when you set it, you're passing a direction, which is transformed in Quarternions and applied to your rotation, using a Vector3.up as reference. this in 2d of course. –  Sep 26 '18 at 20:41
3

Transform object automatically calculates a quaternion rotation using this up Vector like from default Vector3.up to new transform up vector.

ashemez
  • 98
  • 1
  • 6
  • Could you please make your answer a little more clear? – BeastCoder Sep 25 '18 at 23:15
  • When you set the Transform.up property to a new value (a new direction vector) transform automatically sets the Transform.rotation property using Quaternion.FromToRotation(Vector3.up, newTransformUpValue). That’s all. – ashemez Sep 25 '18 at 23:26
  • Does it affect anything else or only rotation? Sorry for the inconvenience. Also how does the vector created in the video make a direction that is facing towards the mouse, sorry. – BeastCoder Sep 26 '18 at 00:17
  • it only affects rotation. Direction is calculated as pointVector2 - pointVector1 where pointVector2 is the point where direction is looking at and pointVector1 is the point where direction starts at in that case point2 is mouse' clicked position, point1 is object's position. World space is actually the world matrix for determining the objects space (matrix) into a rotated, scaled and translated position of the object we are dealing with. So Transform can be considered the world matrix of the object which is calculated from these rotation, scale and position matrices. – ashemez Sep 26 '18 at 07:41
  • If you are curious about what is going on with the Math behind the Transform, world space and camera I suggest you to have a look at the XNA world which used to want the game developer deal with these matrixes and transformations individually at a lower level. Currently Monogame framework is still using the XNA legacy. – ashemez Sep 26 '18 at 07:43