Bullet AI
using UnityEngine;
using System.Collections;
public class bulletAI : MonoBehaviour {
private bool inUse;
private float TIME_ALIVE = 1;
private Vector3 disablePos;
private Vector3 direction;
private float timeAlive;
// Use this for initialization
void Start () {
inUse = false;
disablePos = this.GetComponent<Transform>().position;
timeAlive = 0;
}
// Update is called once per frame
void Update () {
if(inUse)
{
Vector3 pos = this.GetComponent<Transform>().position;
pos += direction * Time.deltaTime * 30;
this.GetComponent<Transform>().position = pos;
timeAlive += Time.deltaTime;
if(timeAlive > TIME_ALIVE)
{
timeAlive = 0;
disable ();
}
}
}
void disable()
{
timeAlive = 0;
this.GetComponent<Transform>().position = disablePos;
inUse = false;
}
public bool isFree()
{
return !inUse;
}
public void shoot(Vector3 start, Vector3 target)
{
inUse = true;
this.GetComponent<Transform>().position = start;
direction = target - start;
direction.Normalize();
timeAlive = 0;
}
void OnTriggerEnter(Collider other)
{
disable();
}
}
No comments:
Post a Comment