Advertisement
Guest User

Untitled

a guest
Jul 17th, 2019
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.09 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class MoveCommand : CharacterCommand
  6. {
  7. private const float MIN_DISTANCE = 0.1f;
  8. /// <summary>
  9. /// assume that character's move speed = 2 m/s
  10. /// </summary>
  11. private const float SPEED = 2f;
  12.  
  13. public Vector2 targetPos { get; private set; }
  14.  
  15. private Animator anim;
  16. private Transform transform;
  17.  
  18. public MoveCommand(GameObject owner, Vector2 targetPos) : base(owner)
  19. {
  20. this.targetPos = targetPos;
  21. anim = owner.GetComponent<Animator>();
  22. transform = owner.transform;
  23. }
  24.  
  25.  
  26. public override IEnumerator Excecute()
  27. {
  28. if (anim == null)
  29. yield break;
  30.  
  31. //TODO: Play anim Move here.
  32.  
  33. while (Vector2.Distance(transform.position, targetPos) > MIN_DISTANCE)
  34. {
  35. Vector2 direction = (targetPos - new Vector2(transform.position.x, transform.position.y)).normalized;
  36. owner.transform.Translate(direction * SPEED * Time.deltaTime);
  37. yield return null;
  38. }
  39.  
  40. Debug.Log("MoveFinish");
  41. }
  42.  
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement