Answer the question
In order to leave comments, you need to log in
How to make a laser effect?
I have a turret that shoots projectiles (TD type game), here is an implementation example
public class TowerBullet : MonoBehaviour{
public float FindRadius = 2f;
public float TimeShot = 0.5f;
Enemy enemy;
Transform towerHead;
private float timerShot = 0f;
public GameObject bullet;
void Start(){
towerHead = transform.Find("Head");
}
void Update(){
if(enemy == null) FindEnemy();
else{
towerHead.LookAt(enemy.transform);
shot();
float dist = Vector3.Distance(enemy.transform.position, transform.position);
if(dist > FindRadius) enemy = null;
}
}
public void shot(){
timerShot -= Time.deltaTime;
if(timerShot <= 0){
timerShot = TimeShot;
GameObject obj = (GameObject)Instantiate(bullet, towerHead.transform.position, towerHead.transform.rotation);
Bullet b = obj.GetComponent<Bullet>();
b.Enemy = enemy;
}
}
public void FindEnemy(){
GameObject[] enemies = GameObject.FindGameObjectsWithTag("Enemy") as GameObject[];
float min = FindRadius;
Enemy minEnemy = null;
foreach(GameObject e in enemies){
float dist = Vector3.Distance(e.transform.position, transform.position);
if(dist <= min){
min = dist;
minEnemy = e.GetComponent<Enemy>();
}
}
enemy = minEnemy;
}
}
public class Bullet : MonoBehaviour{
public float Speed = 15f;
public float TimeLife = 0.1f;
float timerLife = 0f;
public Enemy Enemy;
public float Damage = 25f;
public float Accuracy = 90f;
public float Critical = 2f;
public float Chance_Critical = 20f;
void Start(){
timerLife = TimeLife;
}
void Update(){
timerLife -= Time.deltaTime;
float _speed = Speed * Time.deltaTime;
if(timerLife <= 0){
timerLife = TimeLife;
Object.Destroy(gameObject);
}else if(Enemy != null && Vector3.Distance(transform.position, Enemy.transform.position) <= _speed){
Enemy.SetDamage(Damage);
Object.Destroy(gameObject);
return;
}
transform.Translate(new Vector3(0,0, _speed));
}
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question