- Home /
This post has been wikified, any user with enough reputation can edit it.
how do i use a raycast to hit one target to cause a explosion but hit a second target to cause a different explosion?
I have a script. It is a good script but I want it to cause a explosion on one target but cause a different explosion on the second target. Where can I go get a tutorial on the subject or how do I do it? using UnityEngine; using System.Collections;
public class basicLaserbeam : MonoBehaviour
{
LineRenderer line;
public GameObject explosion;
public AudioClip boom;
void Start ()
{
line = gameObject.GetComponent<LineRenderer>();
line.enabled = false;
}
void Update ()
{
if(Input.GetButtonDown("Fire1"))
{
StopCoroutine("FireLaser");
StartCoroutine("FireLaser", 0.25F);
audio.Play();
}
}
IEnumerator FireLaser(float lengthOfTime)
{
line.enabled = true;
var t = Time.time;
while(Input.GetButton("Fire1") && Time.time - t < lengthOfTime)
{
Ray ray = new Ray(transform.position, transform.forward);
RaycastHit hit;
line.SetPosition(0, ray.origin);
if(Physics.Raycast(ray, out hit, 2500))
{
line.SetPosition(1, hit.point);
if(hit.rigidbody)
{
Instantiate(explosion, hit.point, transform.rotation);
Destroy(hit.transform.gameObject);
audio.PlayOneShot(boom, 1F);
}
}
else
line.SetPosition(1, ray.GetPoint(2500));
yield return null;
}
line.enabled = false;
}
}
Comment
Answer by jbecana · Mar 16, 2014 at 10:35 AM
You could créate different prefab explosions and instatiate them depending on a target specific info, like tag :
if (hit.collider.tag == "Enemy A")
{
Instantiate(explosionEnemyA, hit.point, transform.rotation);
}
else if (hit.collider.tag == "Enemy B")
{
Instantiate(explosionEnemyB, hit.point, transform.rotation);
}
Better, you could create arrays for explosionEnemyA, explosionEnemyB, etc.. and pick a random prefab using Mathf.Random.
Your answer
![](https://koobas.hobune.stream/wayback/20220613140353im_/https://answers.unity.com/themes/thub/images/avi.jpg)