Hey, I’m trying to create a laser system where lasers (that are on the same z level as the camera, but not x and y) shoot and rotate towards the mouse.
This is my script:
using System.Collections;
using UnityEngine;
public class LaserShooter : MonoBehaviour
{
public GameObject laser;
GameObject shootingPoints;
GameObject lasers;
private void Start()
{
shootingPoints = GameObject.FindGameObjectWithTag("ShootingPoints");
lasers = GameObject.FindGameObjectWithTag("Lasers");
}
bool db = false;
private void Update()
{
if (Input.GetKeyDown(KeyCode.Mouse0))
{
StartCoroutine(Shoot());
}
}
IEnumerator Shoot()
{
if (!db)
{
db = true;
for (int i = 0; i < shootingPoints.transform.childCount; i++)
{
GameObject point = shootingPoints.transform.GetChild(i).gameObject;
GameObject laserClone = Instantiate(laser);
laserClone.transform.SetParent(lasers.transform, false);
laserClone.transform.position = point.transform.position;
}
yield return new WaitForSeconds(0.05f);
for (int i = 0; i < lasers.transform.childCount; i++)
{
Destroy(lasers.transform.GetChild(i).gameObject);
}
yield return new WaitForSeconds(0.1f);
db = false;
}
}
}
How would I do this? I’ve tried using LookAt, but I couldn’t do it.