- Home /
Instantiate / transform.forward not working properly,Instantiate - Transform.position + Transform.forward gets further away if I am moving? Why?
Instantiate (enemies [EnemyIdentity], transform.position + transform.forward * 2 + transform.TransformPoint (0, 0, 0), Quaternion.Euler(transform.rotation.x,transform.rotation.y,transform.rotation.z));
For some reason, when I am holding in w (run forwards), the object being instantiated spawns much further away than when I stay still. Why is this?
Thanks in advance.,Instantiate (enemies [EnemyIdentity], transform.position + transform.forward * 2 + transform.TransformPoint (0, 0, 0), Quaternion.Euler(transform.rotation.x,transform.rotation.y,transform.rotation.z));
For some reason, when I am holding the w key (forwards) and my player is running, the enemy that is spawned is spawned much further away than if I am not pressing anything. Why is this?
Thanks in advance.
I removed transform.TransformPoint (0, 0, 0) which seemed to work for some reason. However, the instantiated object still isn't facing the same way as my character.
maybe you already solved this, but, why used
Quaternion.Euler(transform.rotation.x,transform.rotation.y,transform.rotation.z)
this is the same to write
transform.rotation
and why write
transform.TransformPoint (0, 0, 0)
this is (theorically) the same to write
transform.position
so, maybe kiss principle would help to not mess with a complex code?
Answer by Bunny83 · Jan 27, 2019 at 02:39 AM
You have a lot misconceptions here.Like DCordoba mentioned above transform.TransformPoint (0, 0, 0)
is exactly the same as transform.position
. Since you use both and add them together you get twice the offset from the world origin.
Second is this line make no sense whatsoever:
Quaternion.Euler(transform.rotation.x,transform.rotation.y,transform.rotation.z)
transform.rotation
is a Quaternion and does not represent euler angles. A quaternion is a 4 dimensional complex number system and has the components x,y,z and w. Since we use unit quaternions to represent rotations / orientations those are in the range of -1 to 1. Quaternion.Euler on the other hand expects 3 euler angles in degree (0 to 360 or -180 to 180) and will create a quaternion from those.
If you want the instantiated object to be rotated exactly the same as the object that instantiates the object, just pass transform.rotation
as rotation. So just do:
Instantiate (enemies [EnemyIdentity], transform.position + transform.forward * 2, transform.rotation);