How to change the position of the rotated transfom
I have a question about changing the position of a rotated transform. For example,
I rotated the object shown in the picture around the x-axis. Then I want to move it along the y-axis (yellow arrow). For this I use a code like
transform.localPosition = new Vector3(transform.position.x, newY, transform.position.z);
where newY stands for the new y value that I want to pass to the object transformation.
However, the result is obtained as
The object appears to be moving around the y-axis. So how can I let it go with my own y-axis (yellow arrow)?
source to share
The reason is that the object shown in the figure does not have a parent. In this case, transform.localPosition is the same as transform.position.
You can calculate your own y-axis direction on the world axis and then move it. For example, you can use Transform.TransformDirection to transform a direction from local space to world space.
Vector3 RelativeDirection = gameObject.transform.TransformDirection(0, 1, 0);
RelativeDirection may be desirable.
source to share