- Home /
Problems with a shooting script
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:
Answer by Apinaheebo · Feb 12, 2013 at 03:34 PM
public GameObject projPrefab;
You need to drag your projectile prefab (if u have made one) to that in the inspector window. At the moment it doesn't know what it should instantiate. So for example, make a new cube, make a prefab of it, drag that prefab to the projPrefab in the inspector window. Then it will shoot cubes.
Your answer
![](https://koobas.hobune.stream/wayback/20220613092556im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How to add sound to gun shot script 4 Answers
How can I shoot bullets that you can aim with the crosshair? 1 Answer
FPS PISTOL 1 Answer
Problem with firing laser in direction of camera 1 Answer
Shooting script problem. 2 Answers