Skip to content

Instantly share code, notes, and snippets.

@poga
Created September 22, 2013 08:43
Show Gist options
  • Save poga/6658015 to your computer and use it in GitHub Desktop.
Save poga/6658015 to your computer and use it in GitHub Desktop.
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