Hello,
I am experiencing an issue with the following scripts. I am intermediate at scripting (so pardon any mistakes in the code). The following scripts work correctly when the camera is at a 90 degree angle, however when i shift that angle to 45 degree’s, the script will only fire on the x axis. I want y Axis to be 0, because my game will not enable people to move up and down. As i understand, I need the z axis to do this, however Screen to world does not return z Values.
This is a unity 3d game, but my movement will only be on the x/z axis (assuming im explaining that correctly, my ship will only move left right forward and back, not up in down, resulting in a 3d space, with 2d movement (if that makes anysense).
using System.Collections;
using UnityEngine;
public class FireToMouse : MonoBehaviour
{
public Attributes primary;
public GameObject fp1;
public GameObject fp2;
public GameObject fp3;
public GameObject fp4;
public GameObject fp5;
public GameObject Bullet;
public GameObject LaserImpactGFX;
public GameObject chargeGFX;
public float shootTimer = 0.5f;
public float timeBetweenShots = 0.5f;
public float CoolDown = 0.5f;
public int energycost = 25;
public float WeaponsPause = 3;
public float ReloadTime = 3;
public int MinDamage = 50;
public int MaxDamage = 100;
public bool cannonsActive;
public bool lasersActive;
public bool missilesActive;
public bool CanFire = true;
public bool missiles;
public bool cannons;
public bool lasers;
public int shots;
public Vector3 target;
public LineRenderer line;
public void Start()
{
line.enabled = false;
chargeGFX.transform.gameObject.SetActive(false);
LaserImpactGFX.transform.gameObject.SetActive(false);
}
public void Update()
{
target = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, transform.position.z));
target.y = 0f;
Fireatmouse();
FireCannons(WeaponsPause);
FireLaser(WeaponsPause);
FireMissile(WeaponsPause);
if (line.enabled == true)
{
line.SetPosition(0, new Vector3(fp1.transform.position.x, fp1.transform.position.y, fp1.transform.position.z));
line.SetPosition(1, new Vector3(target.x, target.y, target.z));
chargeGFX.transform.gameObject.SetActive(true);
}
else
{
line.enabled = false;
chargeGFX.transform.gameObject.SetActive(false);
LaserImpactGFX.transform.gameObject.SetActive(false);
}
}
public void Fireatmouse()
{
CoolDown -= Time.deltaTime;
if (CoolDown <= 0)
{
CoolDown = 0;
}
if (CoolDown <= 0)
{
CanFire = true;
}
else
{
CanFire = false;
if (CanFire == false)
{
return;
}
}
CoolDown = ReloadTime;
//turn on cannons trigger
if (Input.GetMouseButton(0) && cannons && CanFire == true)
{
StartCoroutine("FireCannons", WeaponsPause);
}
//turn on laser trigger
if (Input.GetMouseButton(0) && lasers)
{
lasersActive = true;
}
//shutoff weapons
if (Input.GetMouseButton(0) == false)
{
cannonsActive = false;
lasersActive = false;
}
//start laser coroutine
if (lasersActive == true)
{
StartCoroutine("FireLaser", WeaponsPause);
line.enabled = true;
}
else if (lasersActive == false)
{
line.enabled = false;
}
if (missiles == true)
{
StartCoroutine("FireMissile", WeaponsPause);
}
}
public IEnumerator FireLaser(float WeaponsPause)
{
RaycastHit hit;
if (Physics.Raycast(fp1.transform.position, target - fp1.transform.position, out hit))
{
switch (hit.transform.gameObject.tag)
{
case "NPC":
{
LaserImpactGFX.transform.gameObject.SetActive(true);
LaserImpactGFX.transform.position = target;
hit.transform.gameObject.GetComponent<TakeDamage>().RecieveDamage(Random.Range(MinDamage, MaxDamage));
yield return new WaitForSeconds(5);
//creates an graphics effect for where the laser is hitting.
break;
}
case "Asteroids":
{
LaserImpactGFX.transform.gameObject.SetActive(true);
LaserImpactGFX.transform.position = target;
hit.transform.gameObject.GetComponent<TakeDamage>().RecieveDamage(Random.Range(MinDamage, MaxDamage));
yield return new WaitForSeconds(5);
//creates an graphics effect for where the laser is hitting.
break;
}
}
}
// turns off the laser when completed
//FireSFX.Stop();
line.enabled = false;
StopCoroutine("FireLaser");
}
public IEnumerator FireCannons(float WeaponsPause)
{
shots = Random.Range(1, 5);
if (shots == 1)
{
primary.CurrentEnergy -= energycost;
GameObject b = GameObject.Instantiate(Bullet, fp1.transform) as GameObject;
b.transform.parent = null;
yield return new WaitForSeconds(WeaponsPause);
}
else if (shots == 2)
{
primary.CurrentEnergy -= energycost;
GameObject b = GameObject.Instantiate(Bullet, fp2.transform);
b.transform.parent = null;
yield return new WaitForSeconds(WeaponsPause);
}
else if (shots == 3)
{
primary.CurrentEnergy -= energycost;
GameObject b = GameObject.Instantiate(Bullet, fp3.transform);
b.transform.parent = null;
yield return new WaitForSeconds(WeaponsPause);
}
else if (shots == 4)
{
primary.CurrentEnergy -= energycost;
GameObject b = GameObject.Instantiate(Bullet, fp4.transform);
b.transform.parent = null;
yield return new WaitForSeconds(WeaponsPause);
}
else if (shots == 5)
{
primary.CurrentEnergy -= energycost;
GameObject b = GameObject.Instantiate(Bullet, fp5.transform);
b.transform.parent = null;
yield return new WaitForSeconds(WeaponsPause);
}
if (shots >= 5)
{
shots = 5;
}
if (shots <= 1)
{
shots = 1;
}
yield return new WaitForSeconds(WeaponsPause);
StopCoroutine("FireCannons");
}
public IEnumerator FireMissile(float WeaponsPause)
{
shots = Random.Range(1, 5);
if (shots == 1)
{
primary.CurrentEnergy -= energycost;
GameObject b = GameObject.Instantiate(Bullet, fp1.transform) as GameObject;
b.transform.parent = null;
yield return new WaitForSeconds(WeaponsPause);
}
else if (shots == 2)
{
primary.CurrentEnergy -= energycost;
GameObject b = GameObject.Instantiate(Bullet, fp2.transform);
b.transform.parent = null;
yield return new WaitForSeconds(WeaponsPause);
}
else if (shots == 3)
{
primary.CurrentEnergy -= energycost;
GameObject b = GameObject.Instantiate(Bullet, fp3.transform);
b.transform.parent = null;
yield return new WaitForSeconds(WeaponsPause);
}
else if (shots == 4)
{
primary.CurrentEnergy -= energycost;
GameObject b = GameObject.Instantiate(Bullet, fp4.transform);
b.transform.parent = null;
yield return new WaitForSeconds(WeaponsPause);
}
else if (shots == 5)
{
primary.CurrentEnergy -= energycost;
GameObject b = GameObject.Instantiate(Bullet, fp5.transform);
b.transform.parent = null;
yield return new WaitForSeconds(WeaponsPause);
}
if (shots >= 5)
{
shots = 5;
}
if (shots <= 1)
{
shots = 1;
}
yield return new WaitForSeconds(WeaponsPause);
StopCoroutine("TurretFire");
}
}
[code=CSharp]using UnityEngine;
public class BulletScript : MonoBehaviour
{
FireToMouse ftm;
public float speed = 350f;
public int MinDamage = 100;
public int MaxDamage = 200;
public GameObject ImpactGFX;
public AudioSource AudioSFXImpact;
GameObject bullet;
public GameObject muzzleGFX;
public float duration = 3f;
public float autoDestruct = 1;
Vector3 target;
Vector3 dir;
// Start is called before the first frame update
void Start()
{
GameObject.FindGameObjectWithTag("MainCamera").GetComponent<Camera>();
target = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, transform.position.z));
target.y = 0f;
bullet = this.gameObject;
transform.LookAt(target);
}
// Update is called once per frame
void Update()
{
float step = speed * Time.deltaTime;
Vector3 dir = transform.forward * 100 * step;
if (Vector3.Distance(this.transform.position, target) > autoDestruct)
{
transform.Translate(dir.normalized * step, Space.World);
}
else if (Vector3.Distance(this.transform.position, target) <= autoDestruct)
{
Destroy(this.gameObject);
}
duration -= Time.deltaTime;
if (duration <= 0)
{
Destroy(bullet);
}
if(Vector3.Distance(bullet.transform.position, target) <= autoDestruct)
{
}
}
void OnDrawGizmosSelected()
{
Gizmos.color = Color.red;
Gizmos.DrawWireSphere(transform.position, autoDestruct);
}
void ApplyForce(Rigidbody bullet)
{
}
void OnTriggerEnter(Collider collision)
{
if (collision.gameObject.tag == "NPC")
{
collision.gameObject.GetComponent<TakeDamage>().RecieveDamage(Random.Range(MinDamage, MaxDamage));
Destroy(this.bullet);
}
if (collision.gameObject.tag == "Asteroids")
{
collision.gameObject.GetComponent<TakeDamage>().RecieveDamage(Random.Range(MinDamage, MaxDamage));
Destroy(this.bullet);
}
}
}
[/code]