- Home /
unity3d add float to forward
Vector3 pos;
void Update(){
if(Input.GetKeyDown(KeyCode.E)){
pos = transform.parent.position;
pos.z += 2;
}
}
Why does this not work???
Comment
How it doesn't work? At least there is a conflict with types: Vector3 operates with floats and 2 is an integer. Try something like pos.z += 2.0f;
Thank you for your answer! but I had to assign the transform of the parent after changing the transform of the value, as in the answer below :)
Best Answer
Answer by rutter · Aug 11, 2014 at 06:13 PM
When you access the transform's position, that is "passed by value". You get a copy of the original value, and changes made to the copy won't directly alter the original.
You can modify the value and assign a new position:
pos = transform.parent.position;
pos.z += 2;
transform.parent.position = pos;