Guest User

Untitled

a guest
Jan 18th, 2018
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.96 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. [RequireComponent(typeof(GamePiece))]
  6. public class MovablePiece : MonoBehaviour {
  7.  
  8. private GamePiece piece;
  9. private IEnumerator moveCoroutine;
  10.  
  11. private void Awake()
  12. {
  13. piece = GetComponent<GamePiece>();
  14. }
  15.  
  16. public void Move(float time, int newX, int newY)
  17. {
  18. moveCoroutine = MoveCoroutine(time, newX, newY);
  19. StartCoroutine(moveCoroutine);
  20. }
  21.  
  22. private IEnumerator MoveCoroutine(float time, int newX, int newY)
  23. {
  24. piece.X = newX;
  25. piece.Y = newY;
  26.  
  27. Vector3 startPos = transform.position;
  28. Vector3 endPos = piece.GridRef.GetWorldPosition(newX, newY, Grid.Layer.NORMAL);
  29. for (float t = 0; t <= 1 * time; t += Time.deltaTime )
  30. {
  31. piece.transform.position = Vector3.Lerp(startPos, endPos, t / time);
  32. yield return 0;
  33. }
  34.  
  35. piece.transform.position = endPos;
  36. }
  37. }
Add Comment
Please, Sign In to add comment