- Home /
Question by
Andrea90 · May 05, 2018 at 01:10 PM ·
movement scriptcharacter movement
Double movement speed moving in two directions
Hi guys, I have a problem with the speed of my character when I press two directions simultaneously . I undestand that the issue is given because it gives the speed from both keys to that direction but I can't figure it ou how to solve it.
public class Movement : MonoBehaviour {
public void Move (Vector2 direction)
{
transform.position +=
transform.forward * direction.x * Time.deltaTime +
transform.right * direction.y * Time.deltaTime;
}
}
I call the Move method inside the Player script
public class Player : MonoBehaviour
{
[SerializeField] float speed = 4;
Input input;
Movement movement;
void Start()
{
input = GetComponent<Input>();
movement= GetComponent<Movement>();
}
void Update()
{
Vector2 direction = new Vector2(input.Vertical * speed,
input.Horizontal * speed);
movement.Move(direction);
}
}
Thank you in advance for any help or advice you will give me
Comment
With WASD keys everything works fine if a move in just one direction but if I move diagonally for example forward and right (WD keys) the speed is double...
Answer by TanselAltinel · May 06, 2018 at 01:19 PM
In your Move method, instead of pushing your player to summation of vectors, do this:
public void Move (Vector2 direction)
{
transform.position = new Vector2( transform.position.x + direction.x, transform.position.y + direction.y);
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612162051im_/https://answers.unity.com/themes/thub/images/avi.jpg)