I’m having a problem with a shooting script that I got off of the Unity wiki.
“Weapon Script”
public class weaponScript : MonoBehaviour
{
// public
public float projMuzzleVelocity; // in metres per second
public GameObject projPrefab;
public float RateOfFire;
public float Inaccuracy;
// private
private float fireTimer;
// Use this for initialization
void Start ()
{
fireTimer = Time.time + RateOfFire;
}
// Update is called once per frame
void Update ()
{
Debug.DrawLine(transform.position, transform.position + transform.forward, Color.red);
if (Time.time > fireTimer)
{
GameObject projectile;
Vector3 muzzlevelocity = transform.forward;
if (Inaccuracy != 0)
{
Vector2 rand = Random.insideUnitCircle;
muzzlevelocity += new Vector3(rand.x, rand.y, 0) * Inaccuracy;
}
muzzlevelocity = muzzlevelocity.normalized * projMuzzleVelocity;
projectile = Instantiate(projPrefab, transform.position, transform.rotation) as GameObject;
projectile.GetComponent<projectileScript>().muzzleVelocity = muzzlevelocity;
fireTimer = Time.time + RateOfFire;
}
else
return;
}
}
“Projectile Script”
public class projectileScript : MonoBehaviour
{
public Vector3 muzzleVelocity;
public float TTL;
public bool isBallistic;
public float Drag; // in metres/s lost per second.
// Use this for initialization
void Start ()
{
if (TTL == 0)
TTL = 5;
print(TTL);
Invoke("projectileTimeout", TTL);
}
// Update is called once per frame
void Update ()
{
if (Drag != 0)
muzzleVelocity += muzzleVelocity * (-Drag * Time.deltaTime);
if (isBallistic)
muzzleVelocity += Physics.gravity * Time.deltaTime;
if (muzzleVelocity == Vector3.zero)
return;
else
transform.position += muzzleVelocity * Time.deltaTime;
transform.LookAt(transform.position + muzzleVelocity.normalized);
Debug.DrawLine(transform.position, transform.position + muzzleVelocity.normalized, Color.red);
}
void projectileTimeout()
{
DestroyObject(gameObject);
}
}
I’m relatively new to Unity, and I have minimal experience with C sharp. To my knowledge, this script was made for Unity 3 however I think it should work in Unity 4. I keep on getting an error message that says “projPrefab is not defined”. How can I fix this problem? Should I find another script? How do I define “projPrefab”? The link to the page where I got this script is: