Mendenbarr

Mover

Apr 15th, 2016
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.99 KB | None | 0 0
  1. public class CharacterMover : MonoBehaviour {
  2.  
  3.     // Where we want our character to go
  4.     private Vector2 destination;
  5.     // How fast we want our character to move
  6.     public float speed;
  7.  
  8.     void Awake () {
  9.         // Sets the destination to the current postion at the start
  10.         destination.x = this.gameObject.transform.position.x;
  11.         destination.y = this.gameObject.transform.position.y;
  12.     }
  13.  
  14.     void Update () {
  15.         // If the user holds down directional input, the destination changes by 1 in the proper direction
  16.         if (Input.GetKeyDown("down")) destination.y = destination.y - 1;
  17.         if (Input.GetKeyDown("up")) destination.y = destination.y + 1;
  18.         if (Input.GetKeyDown("left")) destination.x = destination.x - 1;
  19.         if (Input.GetKeyDown("right")) destination.x = destination.x + 1;
  20.  
  21.         // If the destination isn't at the postion, move towards the postion
  22.         if (this.gameObject.transform.position != new Vector3(destination.x, destination.y, 0)) {
  23.             // Code to move towards the destination by the speed
  24.         }
  25.     }
  26.  
  27. }
Advertisement
Add Comment
Please, Sign In to add comment