Hi, i need help with grid movement.
The player moves himselfs and he react at a direction changes. He can move at four directions (up, down, right, left), but not in a grid. There is code:
using UnityEngine;
using System.Collections;
public enum Direction { Right, Up, Left, Down };
public class NewBehaviourScript : MonoBehaviour {
public const float speed = 2.0f;
public Direction direction = Direction.Right;
void Update()
{
if (Input.GetKey(KeyCode.RightArrow))
{
direction = Direction.Right;
}
else if (Input.GetKey(KeyCode.UpArrow))
{
direction = Direction.Up;
}
else if (Input.GetKey(KeyCode.DownArrow))
{
direction = Direction.Down;
}
else if (Input.GetKey(KeyCode.LeftArrow))
{
direction = Direction.Left;
}
Vector3 pos = transform.position;
if (direction == Direction.Right)
{
pos += Vector3.right;
}
else if (direction == Direction.Up)
{
pos += Vector3.up;
}
else if (direction == Direction.Left)
{
pos += Vector3.left;
}
else if (direction == Direction.Down)
{
pos += Vector3.down;
}
transform.position = Vector3.MoveTowards(transform.position, pos, speed * Time.deltaTime);
}
}
I need the player to move along the grid.