Z
Z
Zimaell2020-06-26 17:45:30
Unity
Zimaell, 2020-06-26 17:45:30

How to make a laser effect?

I have a turret that shoots projectiles (TD type game), here is an implementation example

spoiler
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));
    }
  }

A TowerBullet is attached to the tower, and everything works as it should to the Bullet bullets themselves - the tower tracks down the enemy, the bullets fly out and disappear when they hit, while causing the necessary damage.

How can I modify the script so that this tower fires a laser? (a beam that will start from the tower to the enemy and stretch when removed until it leaves the radius)

Answer the question

In order to leave comments, you need to log in

2 answer(s)
4
4xy0, 2020-06-26
@4xy0

Try to look here about the beam (just on the example of TD)

G
Griboks, 2020-06-27
@Griboks

1. Create a beam model
2. Stretch and rotate it
3. Repeat n2 in update

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question