Created
September 22, 2013 08:43
-
-
Save poga/6658015 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using System; | |
using System.Collections; | |
public class MoveToAttackCommand : ICommand { | |
public GameObject target; | |
public MoveToAttackCommand(GameObject target) { | |
this.target = target; | |
} | |
public IEnumerator ExecuteOn(GameObject actor) { | |
UnitProperties unit = actor.GetComponent<UnitProperties>(); | |
if (unit == null) { | |
throw(new Exception("Can't execute on object without UnitProperties")); | |
} | |
actor.animation.Play("run"); | |
while (Vector3.Distance(actor.transform.position, target.transform.position) > unit.attackRange) { | |
Vector3 moveTowardPos = Vector3.MoveTowards(actor.transform.position, | |
target.transform.position, | |
unit.moveSpeed * Time.deltaTime); | |
actor.transform.position = moveTowardPos; | |
Quaternion direction = Quaternion.LookRotation( | |
Vector3.RotateTowards(actor.transform.forward, | |
target.transform.position - actor.transform.position, | |
unit.rotationSpeed * Time.deltaTime, | |
0.0f)); | |
actor.transform.rotation = direction; | |
yield return null; | |
} | |
actor.animation.Play("attack"); | |
yield return new WaitForSeconds(unit.attackStartDelay); | |
target.animation.Play("gethit"); | |
yield break; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment