Question by
EtherealPineapple · Apr 24, 2016 at 08:41 PM ·
c#scripting problemunitsyaxis
C# | How do I move an object a number of units?
Right, so I am familiar with transform.Translate but I want to be able to move an object up by 5 units, how do I do that in a script?
Comment
Best Answer
Answer by Oribow · Apr 24, 2016 at 08:44 PM
transform.Translate(Vector3.up * 5);
This will instantly move something 5 units up. For other directions, change the Vector part. For other lengths change the number.
Under what void do I use this on? When I used void Update, it didn't move by how much I wanted, it moved by more than that, and that's not what I want. In my script, I need the gun to move 1.2 units to the left, even if I am holding the right mouse button. Here's my script for a gun ai$$anonymous$$g:
public bool isAi$$anonymous$$g = false;
if(isAi$$anonymous$$g = false && Input.Get$$anonymous$$ey($$anonymous$$eyCode.$$anonymous$$ouse1)){
transform.Translate (Vector3.left * 1.24822076f);
isAi$$anonymous$$g = true;
}
if(isAi$$anonymous$$g = true && Input.Get$$anonymous$$ey($$anonymous$$eyCode.$$anonymous$$ouse1)){
transform.Translate (Vector3.right * 1.24822076f);
isAi$$anonymous$$g = false;
}
}
}
I answered your separate question about this.
You should mark this guy your answer though , he was correct.
Your answer
![](https://koobas.hobune.stream/wayback/20220612075532im_/https://answers.unity.com/themes/thub/images/avi.jpg)