Hi everyone! I have a question on the Laser Defender course, instantiating lasers.
The problem that I’m running into is that my lasers are instantiating directly on top of my player ship, hitting it, and essentially “blowing myself up”.
The weird thing is, this only started after I switched my project from Mac to Windows. Any ideas why this could be, and how to fix it?
Here is the screenshot:
Here is the code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class weaponConfig : MonoBehaviour {
[Header("Projectile")]
[SerializeField] GameObject laserPrefab;
[SerializeField] float projectileSpeed = 10f;
[SerializeField] float projectileFiringPeriod = 0.1f;
[SerializeField] AudioClip shootSound;
[SerializeField] [Range(0, 1)] float shootSoundVolume = 0.25f;
[SerializeField] public static int numberOfLasers = 1;
Coroutine firingCoroutine;
int bBodyCounter;
public float GetProjectileSpeed()
{
return projectileSpeed;
}
public float GetProjectileFiringPeriod()
{
return projectileFiringPeriod;
}
// Update is called once per frame
void Update () {
Fire();
}
public void Fire()
{
if (Input.GetButtonDown("Fire1"))
{
firingCoroutine = StartCoroutine(FireContinuously());
}
if (Input.GetButtonUp("Fire1"))
{
StopCoroutine(firingCoroutine);
}
}
private IEnumerator FireContinuously()
{
while (true)
{
if (numberOfLasers == 1)
{
FireOne();
}
if (numberOfLasers == 2)
{
FireTwo();
}
if (numberOfLasers == 3)
{
FireThree();
}
if (numberOfLasers == 4)
{
FireFour();
}
if (numberOfLasers == 5)
{
FireFive();
}
yield return new WaitForSeconds(projectileFiringPeriod);
}
}
private void FireOne()
{
FireMiddle();
}
private void FireTwo()
{
FireMiddle();
DoubleFire();
}
private void FireThree()
{
FireLeft();
FireRight();
FireMiddle();
}
private void FireFour()
{
FireLeft();
FireRight();
DoubleFire();
DoubleFireNegative();
}
private void FireFive()
{
FireLeft();
DoubleFire();
DoubleFireNegative();
FireRight();
FireMiddle();
}
private void FireLeft()
{
GameObject projectile = Instantiate(laserPrefab, transform.position, Quaternion.identity) as GameObject;
projectile.GetComponent<Rigidbody2D>().velocity = new Vector2(-2, projectileSpeed);
}
private void FireRight()
{
GameObject projectile = Instantiate(laserPrefab, transform.position, Quaternion.identity) as GameObject;
projectile.GetComponent<Rigidbody2D>().velocity = new Vector2(2, projectileSpeed);
}
private void FireMiddle()
{
GameObject projectile = Instantiate(laserPrefab, transform.position, Quaternion.identity) as GameObject;
projectile.GetComponent<Rigidbody2D>().velocity = new Vector2(0, projectileSpeed);
}
private void DoubleFire()
{
GameObject projectile = Instantiate(laserPrefab, transform.position, Quaternion.identity) as GameObject;
projectile.GetComponent<Rigidbody2D>().velocity = new Vector2(1, projectileSpeed);
}
private void DoubleFireNegative()
{
GameObject projectile = Instantiate(laserPrefab, transform.position, Quaternion.identity) as GameObject;
projectile.GetComponent<Rigidbody2D>().velocity = new Vector2(-1, projectileSpeed);
}
}
(I know the firing power-up structure is not the best lol)