Advertisement
Guest User

Playermovement.cs

a guest
Feb 19th, 2019
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.69 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class PlayerMovement : MonoBehaviour {
  5.  
  6.     Direction currentDir;
  7.     Vector2 input;
  8.     bool isMoving = false;
  9.     Vector3 startPos;
  10.     Vector3 endPos;
  11.     float t;
  12.  
  13.     public Sprite northSprite;
  14.     public Sprite eastSprite;
  15.     public Sprite southSprite;
  16.     public Sprite westSprite;
  17.  
  18.     public float walkSpeed = 3f;
  19.  
  20.     public bool isAllowedToMove = true;
  21.  
  22.     void Start()
  23.     {
  24.         isAllowedToMove = true;
  25.     }
  26.  
  27.     void Update () {
  28.  
  29.         if(!isMoving && isAllowedToMove)
  30.         {
  31.             input = new Vector2(Input.GetAxis("Horizontal"), Input.GetAxis("Vertical"));
  32.             if (Mathf.Abs(input.x) > Mathf.Abs(input.y))
  33.                 input.y = 0;
  34.             else
  35.                 input.x = 0;
  36.  
  37.             if(input != Vector2.zero)
  38.             {
  39.  
  40.                 if(input.x < 0)
  41.                 {
  42.                     currentDir = Direction.West;
  43.                 }
  44.                 if(input.x > 0)
  45.                 {
  46.                     currentDir = Direction.East;
  47.                 }
  48.                 if(input.y < 0)
  49.                 {
  50.                     currentDir = Direction.South;
  51.                 }
  52.                 if (input.y > 0)
  53.                 {
  54.                     currentDir = Direction.North;
  55.                 }
  56.  
  57.                 switch(currentDir)
  58.                 {
  59.                     case Direction.North:
  60.                         gameObject.GetComponent<SpriteRenderer>().sprite = northSprite;
  61.                         break;
  62.                     case Direction.East:
  63.                         gameObject.GetComponent<SpriteRenderer>().sprite = eastSprite;
  64.                         break;
  65.                     case Direction.South:
  66.                         gameObject.GetComponent<SpriteRenderer>().sprite = southSprite;
  67.                         break;
  68.                     case Direction.West:
  69.                         gameObject.GetComponent<SpriteRenderer>().sprite = westSprite;
  70.                         break;
  71.                 }
  72.  
  73.                 StartCoroutine(Move(transform));
  74.             }
  75.  
  76.         }
  77.  
  78.     }
  79.  
  80.     public IEnumerator Move(Transform entity)
  81.     {
  82.         isMoving = true;
  83.         startPos = entity.position;
  84.         t = 0;
  85.  
  86.         endPos = new Vector3(startPos.x + System.Math.Sign(input.x), startPos.y + System.Math.Sign(input.y), startPos.z);
  87.  
  88.         while (t < 1f)
  89.         {
  90.             t += Time.deltaTime * walkSpeed;
  91.             entity.position = Vector3.Lerp(startPos, endPos, t);
  92.             yield return null;
  93.         }
  94.  
  95.         isMoving = false;
  96.         yield return 0;
  97.     }
  98. }
  99.  
  100. enum Direction
  101. {
  102.     North,
  103.     East,
  104.     South,
  105.     West
  106. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement