Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Zola390 · Feb 12, 2013 at 02:59 PM · fpsshootinggunshoot

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:

http://wiki.unity3d.com/index.php/ShootingWeaponScript

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

10 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges