I have a sprite object which I want to move in 8 directions. Left, Right, Up, Down, Left top, Right top, Left down, Right down. The view is 2D with a isometric projection. The difficulties I am having is when the object needs to move diagonally. I need to work out how I can move the sprite in fixed degrees diagonally so its parallel to the isometric world.
I have a diagram to demonstrate.
Here the green arrows are the diagonal movement that I need. The code I have for up, down, left and right movement is already in,
void FixedUpdate ()
{
speed = 10f;
//pos = Camera.main.ViewportToWorldPoint (new Vector3 (0.5F, 0.5F, Camera.main.nearClipPlane));
//transform.position = new Vector3 (pos.x, pos.y, -1F);
if (Input.GetKey (KeyCode.D)) {
///rigidbody2D.AddForce (Vector2.right * speed);
rigidbody2D.velocity = Vector2.right * speed;
} else if (Input.GetKey (KeyCode.A)) {
//rigidbody2D.AddForce (-Vector2.right* speed);
rigidbody2D.velocity = -Vector2.right * speed;
} else if (Input.GetKey (KeyCode.S)) {
//rigidbody2D.AddForce (-Vector2.up * speed);
rigidbody2D.velocity = -Vector2.up * speed;
} else if (Input.GetKey (KeyCode.W)) {
//rigidbody2D.AddForce (Vector2.up * speed);
rigidbody2D.velocity = Vector2.up * speed;
} else {
rigidbody2D.velocity = Vector2.zero;
}
}
I had a look at the Transform class and the various variables and functions but I don't know what to use or start with. This is because I am very new to Unity and maths such as rotations, axis, etc. So a helping direction would be massively appreciated. Thanks.