- Home /
transform position vectorially
How do i transform an object vectorially. As an example i want an cube to go 1 m forward then i write
cube.transform.position = cube.transform.position + new vector3(1,0,0);
but what do i write if the cube has rotatet around one or multiple axes and i still want it to go forward on his x-axes? i am thankfull for any help also i wanna mention that i cant use anything parent-child realated because im using prefabs.
Answer by toddisarockstar · Mar 12, 2016 at 04:39 PM
// Move the object forward along its z axis 1 unit/second.
transform.Translate(Vector3.forward * Time.deltaTime);
usually the z axis is forward. if you wanted to move along the x axis you would say Vector3.right. you can still use parenting after spawning a prefab if that helps. you would just say someting like this
var prefab:GameObject;
var theparent:GameObject;
var ob:GameObject;
ob=Instantiate(prefab,theparent.transform.position, theparent.transform.rotation);
ob.transform.parent=theparent.transform;
Answer by Fydar · Mar 12, 2016 at 04:39 PM
Use:
transform.forward
To find out the forward direction of the cube, rather than the global forward direction of:
new Vector3 (1, 0, 0)
Uhm either change your vector from (1, 0, 0) to (0, 0, 1) or "forward" to "right" because at the moment your answer is wrong. "forward" is the z-axis, "right" is x-axis and "up" is the y-axis.
That is not the point of the answer. The point of the answer is to use transform.forward rather then vector3.forward.