Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class CharacterMover : MonoBehaviour {
- // Where we want our character to go
- private Vector2 destination;
- // How fast we want our character to move
- public float speed;
- void Awake () {
- // Sets the destination to the current postion at the start
- destination.x = this.gameObject.transform.position.x;
- destination.y = this.gameObject.transform.position.y;
- }
- void Update () {
- // If the user holds down directional input, the destination changes by 1 in the proper direction
- if (Input.GetKeyDown("down")) destination.y = destination.y - 1;
- if (Input.GetKeyDown("up")) destination.y = destination.y + 1;
- if (Input.GetKeyDown("left")) destination.x = destination.x - 1;
- if (Input.GetKeyDown("right")) destination.x = destination.x + 1;
- // If the destination isn't at the postion, move towards the postion
- if (this.gameObject.transform.position != new Vector3(destination.x, destination.y, 0)) {
- // Code to move towards the destination by the speed
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment