I am trying to create a more advance weaponScript. I haven’t gotten far at all yet because I am running into a problem “use of unassigned variable” I understand what this means, but I thought it would work be cause the variable “hit” I declared as a RaycastHit. Idk I am doing something wrong though, I know that. So please take a look at the script and tell me what the problems are with it. Also if there is any criticism on how I am going about any part please let me know. I want to do things the cleanest and most efficient way possible.
using UnityEngine;
using System.Collections;
public class weaponScript : MonoBehaviour
{
//Clips And Ammo
public int currentAmmo;
public int clipCapacity;
public int numberOfClips;
//Fire Rate(Add burst)
public float fireRate;
public float fireTime;
public float nextFireTime;
//FX
public GameObject hitStaticFX;
public GameObject hitPlayerFX;
public GameObject bulletHole;
//Functionality.
public int damage;
void Update()
{
//If we try to fire and we are able to
if (Input.GetMouseButton(0) && Time.time >= nextFireTime)
{
//Tell us we can fire.
Debug.Log("Fire Ready");
//Declare a ray, And the ray hitpoint.
Ray ray = Camera.main.ViewportPointToRay(new Vector3(0.5f, 0.5f, 0));
RaycastHit hit;
//if the hit GO's tag is...
switch (hit.transform.tag)
{
case "bullet":
// do nothing if 2 bullets collide
break;
case "Player":
// add blood effect
Instantiate(hitPlayerFX, hit.point, Quaternion.FromToRotation(Vector3.forward, hit.normal));
break;
case "Static":
// add wood impact effects
//Can use this to add a decal where the "projectile" hits.
Instantiate(hitStaticFX, hit.point, Quaternion.FromToRotation(Vector3.forward, hit.normal));
break;
case "ground":
// add dirt or ground impact effect
break;
default: // default impact effect and bullet hole
Instantiate(hitStaticFX, hit.point + 0.1f * hit.normal, Quaternion.FromToRotation(Vector3.up, hit.normal));
GameObject newBulletHole = Instantiate(bulletHole, hit.point, Quaternion.FromToRotation(Vector3.up, hit.normal)) as GameObject;
newBulletHole.transform.parent = hit.transform;
break;
}
//Add time to next fire.
nextFireTime = Time.time + fireRate;
}
}
}