Skip to content
Permalink
961cf0bee1
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Go to file
 
 
Cannot retrieve contributors at this time
53 lines (44 sloc) 1.51 KB
using UnityEngine;
using System.Collections;
public class EnemyAttack_Range : MonoBehaviour
{
#region Field
public float ProjectileSpeed;
public GameObject hero;
private Vector3 selfPosition;
private Vector3 heroPosition;
private float tmpGetOne;
#endregion
// initialization
void Start () {
//hero = (Hero) GameObject.Find("Hero").GetComponent("Hero");
hero = GameObject.FindWithTag("Player");
//destry itself after some time
StartCoroutine(DestroySelf());
//when initializated, find self pos and hero pos to get projectile direction
selfPosition = new Vector3 (transform.position.x, 0, transform.position.z);
heroPosition = new Vector3 (hero.transform.position.x, 0, hero.transform.position.z);
tmpGetOne = Mathf.Sqrt((hero.transform.position.x - transform.position.x) * (hero.transform.position.x - transform.position.x) + (hero.transform.position.z - transform.position.z) * (hero.transform.position.z - transform.position.z));
}
// Update is called once per frame
void Update ()
{
float amtToMove = ProjectileSpeed * Time.deltaTime;
transform.Translate((heroPosition - selfPosition) * amtToMove / tmpGetOne,Space.World );
}
//when hit player, call player gothit func
void OnTriggerEnter(Collider otherObject)
{
//Debug.Log ("got hit");
if (otherObject.tag == "Player")
{
otherObject.gameObject.SendMessage("heroGotHit",1);
}
}
//helper function, disappear after some time
IEnumerator DestroySelf()
{
yield return new WaitForSeconds(1.5f);
Destroy(this.gameObject);
}
}