- Home /
This question was
closed Sep 11, 2018 at 07:58 PM by
Schmeep for the following reason:
The question is answered, right answer was accepted
Move character by X units on the left
if (Input.GetKey(KeyCode.D) && tr.position == pos)
{
pos += Vector3.right;
}
else if (Input.GetKey(KeyCode.A) && tr.position == pos)
{
pos += Vector3.left;
}
Comment
Best Answer
Answer by dan_wipf · Sep 11, 2018 at 07:05 PM
instead of vector3.left you can use
new Vector3(1.25f,0,0);
Or simply
pos += Vector3.left * 1.25f;
or in this particular case we can do
pos.x += 1.25f;
Note that the last example doesn't work with transform.position, only when you have a seperate Vector3 variable
Also note that you usually want to multiply the value by Time.deltaTime to be framerate independent. This however means you most likely have to use a larger speed value. So usually you would do something like this:
pos += Vector3.left * speed * Time.deltaTime;
where "speed" is in "units per second".