Changing myObject.transform.up returns strange results
Hi,
I am trying to set the value of transform.up for my object. I know about the problems related to Vector3 being a struct. The problem here is not that the value is not modified. It is, but in a strange way.
Here's an extract of my script:
// The value of v1 is the result of another function.
// I instantiate it like this for the example
Vector3 v1 = new Vector3 (-15.3, 5.3, -8.8);
// cube is my object
Debug.Log ("Before: " + cube.transform.up);
cube.transform.up += v1;
Debug.Log ("After: " + cube.transform.up);
Debug.Log ("v1: " + v1);
And here's what the console prints:
Before: (0.0, 1.0, 0.0)
After: (-0.8, 0.3, -0.5)
v1: (-15.3, 5.3, -8.8)
I was expecting to have something more like this for the "After" part : (-15.3, 6.3, -8.8)... I really don't understand why it is working this way.
Could anyone help me on this?
Thanks a lot!
Answer by Cam Edwards · Sep 07, 2015 at 02:05 PM
Transform.up represents the direction of the "green axis." To represent directions with vectors, you normalize them so that they end up being 1 unit long.
I did a quick check and (-0.8, 0.3, -0.5) is in fact (-15.3, 5.3, -8.8) normalized, that is, both vectors point in the same direction, except the first one has a length of 1. What you are doing is working fine, Unity is just doing some extra calculations for you.
That's what I figured in the end. But is it possible to tell Unity not to do the normalization?
I have a plane and a cube in my scene. What I want to do is to place the cube at the centre of the plane and rotate it so that the forward vector of the cube is facing the up vector of the plane (the cube will be replaced by a camera later).
However, if I use the normalized version of my vector, it will not work since the position is relative to the origin of the world space...(-15.3, 5.3, -8.8) is the version of my vector translated so that the centre of my plane is the new origin.
I learned in the documentation that Vector3 could be considered as a point and as a vector, but it seems that whenever I work on a Vector3 it is considered as a point.
Is there a simple way to get the direction of a vector and apply it to another one?
Ok, I found a way to do this. Setting my cube as a child of the plane makes it way easier.
cube.transform.parent = plane.transform;
cube.transform.position = plane.transform.position;
cube.transform.forward = -transform.up;
Thank you for your help!
Your answer
![](https://koobas.hobune.stream/wayback/20220612032743im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Terrain height -> loosing Players GO childs 0 Answers
Problem with aiming Script(Only Some Code Works) 0 Answers
How do I specify the origin and end points of the arc1 instantiated line? 0 Answers
Set new default position each time? 1 Answer
How to use Quaternion.Slerp with transform.LookAt? 3 Answers