I’m working on a simple 4x4 grid based game and am coding the scripts in C#. When I implement either of these lines of code they appear to have the same effect, I don’t have them both running at the same time.
transform.Translate(Vector3.forward*Time.deltaTime*6f);
transform.localPosition += transform.forward*Time.deltaTime*6f;
I was wondering if there was a significant difference between the two or if I should use one over the other, thanks!
vexe
June 19, 2014, 4:57pm
3
This is what Translate
literally does:
public void Translate(Vector3 translation, [DefaultValue("Space.Self")] Space relativeTo)
{
if (relativeTo == Space.World)
{
this.position += translation;
}
else
{
this.position += this.TransformDirection(translation);
}
}
I really believe one’s decompiling and taking a peek himself is very educational. Have doubt how XXX work? just take a look…
I use ILSpy , it served me well.
It might sound intimidating but the process goes something like this:
Download and install ILSpy.
Drag/drop the UnityEditor.dll
located in the Editor/Data/Managed
folder in your disk to ILSpy
Browse through, lurk around till you find what you’re looking for.
Well I always use the second format, they are indeed exactly the same presuming that there is no parent.
Translate takes into consideration the rotation of the local object, so it converts Vector3.forward into the direction of the current object.
To get exactly the same effect with or without parents, you would need to do:
transform.position += transform.forward * ....