Permalink
Cannot retrieve contributors at this time
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?
Silverkeep/Guardian of Silverkeep/Assets/Scripts/EnemyAttack_Range.cs
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
53 lines (44 sloc)
1.51 KB
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.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); | |
} | |
} |