- Home /
Question by
sNo73 · Aug 20, 2020 at 06:53 PM ·
c#movement scriptgrid based game
How can i add movement distance into my code?,how can i add distance into this movement code?
Hello, I'm sort of new with player movement. I can't seem to try and make my player move at specific distance. Instead of (1,1,1) each time i hit arrow key, i want it lower, like (0.2,0.2,0.2). Any sort of help is appreciated !
.
private float speed = 2.0f;
private Vector3 pos;
void Start()
{
pos = transform.position;
}
void Update()
{
if (Input.GetKeyDown(KeyCode.RightArrow) && transform.position == pos)
{
pos += Vector3.right;
}
else if (Input.GetKeyDown(KeyCode.LeftArrow) && transform.position == pos)
{
pos += Vector3.left;
}
else if (Input.GetKeyDown(KeyCode.UpArrow) && transform.position == pos)
{
pos += Vector3.up;
}
else if (Input.GetKeyDown(KeyCode.DownArrow) && transform.position == pos)
{
pos += Vector3.down;
}
transform.position = Vector3.MoveTowards(transform.position, pos, Time.deltaTime * speed);
}
}
Comment
Answer by unity_ek98vnTRplGj8Q · Aug 20, 2020 at 08:54 PM
Multiply your direction vector by whatever distance you want to travel, for example
float distance = 0.2f;
pos += (Vector3.right * distance);
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Why does the object not move on the Z-Axis? 0 Answers
Smooth Movement with set distance 1 Answer
[ C#] Character Movement based on Camera Direction. 2 Answers