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 Cobradabest · Feb 24, 2013 at 07:59 PM · c#rotationvelocityprojectile

How can I fire multiple projectiles at once?

I'm making a first person shooter, and I want to make it so that the shotgun fires 2 bullets/darts at once, the problem is, the darts are always facing the same direction, even if I shoot in another, and sometimes, it just freezes in mid-air, sometimes, it stutters in mid-air.

Here's my code:

 using UnityEngine;
 using System.Collections;
 
 public class ObjectPool : MonoBehaviour {
     
     GameObject[] projectiles;
     public GameObject projectileToCreate;
     public int numberOfProjectilesToCreate;
     public RayShoot gunScript;
     
     public float spread;
     public byte spreadAmount;
         
     public enum shootType
     {
         normal = 0,
         spread = 1,
         multiSource = 2
     }
     public shootType typeOfGun;
     public byte lifeSpan;
     
     // Use this for initialization
     void Start () 
     {
         projectiles = new GameObject[numberOfProjectilesToCreate];
         InstantiateProjectiles();
     }
     
     // Update is called once per frame
     void Update () 
     {
         if (gunScript.shooting)
         {
             Activate();
         }
     }
     
     private void InstantiateProjectiles()
     {
         for (int i = 0; i < numberOfProjectilesToCreate; i++)
         {
             Vector3 dartPosition = new Vector3 (transform.position.x, transform.position.y + Random.Range(-spread, spread),
                             transform.position.z + Random.Range(-spread, spread)); 
             Quaternion dartRotation = Quaternion.Euler(transform.eulerAngles.x + Random.Range(-spread, spread), transform.eulerAngles.y +
                             Random.Range(-spread, spread), transform.eulerAngles.z + Random.Range(-spread, spread));
             projectiles[i] = Instantiate(projectileToCreate, dartPosition, dartRotation) as GameObject;
             projectiles[i].GetComponent<ProjectileScript>().speed += Random.Range(-spread, spread);
             projectiles[i].SetActiveRecursively(false);
         }
     }
     
     private void Activate()
     {
             for (int s = 0; s < spreadAmount; s++)
             {
                 for (int i = 0; i < numberOfProjectilesToCreate; i++)
                 {
                     if (!projectiles[i].active)
                     {
                         projectiles[i].transform.position = new Vector3 (transform.position.x, transform.position.y + Random.Range(-spread, spread),
                             transform.position.z + Random.Range(-spread, spread));
                         projectiles[i].transform.rotation = Quaternion.Euler(transform.eulerAngles.x + Random.Range(-spread, spread), transform.eulerAngles.y +
                             Random.Range(-spread, spread), transform.eulerAngles.z + Random.Range(-spread, spread));
                         projectiles[i].SetActiveRecursively(true);
                         projectiles[i].GetComponent<ProjectileScript>().Activate();
                         return;
                     }
                 }
             }                
             }
         }
     }
 }
 -----------------------------------------------------------
 using UnityEngine;
 using System.Collections;
 
 public class ProjectileScript : MonoBehaviour {
     
     public float lifeSpan;
     public float speed;
     private float timeToDie;
     
     // Use this for initialization
     void Start () 
     {
         
     }
     
     // Update is called once per frame
     void Update () 
     {
         CountDown();
         //AutoMove();
     }
     
     public void Activate()
     {
         timeToDie = Time.time + lifeSpan;
         transform.position = Vector3.zero;
         Vector3 velocity = speed * Time.deltaTime * transform.forward;
         transform.Translate(velocity);
     }
     
     private void Deactivate()
     {
         this.gameObject.SetActive(false);
     }
     
     private void CountDown()
     {
         if (timeToDie < Time.time)
         {
             Deactivate();
         }
     }
 }
 

How do I fix this?

Comment
Add comment · Show 3
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
avatar image hoy_smallfry · Feb 24, 2013 at 08:22 PM 0
Share

This is a script that ios attached to your weapon?

What type is the "projectile" variable? a prefab?

avatar image Cobradabest · Feb 24, 2013 at 08:25 PM 0
Share

Oh, I'm sorry, projectile is a rigidbody.

This script is kind of attached to the weapon, not attached to the weapon itself, but attached to an empty that's a child of the weapon.

avatar image hoy_smallfry · Feb 24, 2013 at 08:28 PM 0
Share

so this "projectile" rigidbody is attached to a some 'master' bullet that you are making copies from?

Also, have you tested to make sure that your "dartRotation" quaternion is the proper value?

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by nsxdavid · Feb 24, 2013 at 08:27 PM

transform.rotation is Quaternion. You are seeding your dartRotation Quaternion from Euler angles, however you are randomizing off of components of the Quaternion.

So you probably intended:

 Quaternion dartRotation = Quaternion.Euler(transform.eulerAngles.x + Random.Range(-spread, spread), transform.eulerAngles.y +
                  Random.Range(-spread, spread), transform.eulerAngles.z + Random.Range(-spread, spread));
Comment
Add comment · Show 10 · 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
avatar image Cobradabest · Feb 24, 2013 at 08:34 PM 0
Share

It worked perfectly! Thank you!

Do you have any idea how I can fix the freezing/stuttering in mid-air problem?

avatar image nsxdavid · Feb 24, 2013 at 08:45 PM 0
Share

Not enough code to really look into that.

I will note that your Destroy() call should just have simple seconds as the final argument. If you want the projectile to be deleted in 5 seconds, the last argument should be 5. Frame rate has nothing to do with it.

avatar image Cobradabest · Feb 24, 2013 at 08:50 PM 0
Share

Well, my guns fire fast, and so the game becomes filled with darts, and slows the game right down, the destroy framerate command help sort that, it avoids more bullets co$$anonymous$$g out to slow it down more, and kills the bullets already out quicker, improving the framerate, and it works.

avatar image nsxdavid · Feb 24, 2013 at 08:56 PM 0
Share

Better to keep a count of how many projectiles are in the scene and keep it capped. Either don't allow any more when you hit the limit, or destroy the oldest, etc.

Also consider not using physics if you don't really need it. This might be overkill for a $$anonymous$$igore like game.

avatar image nsxdavid · Feb 25, 2013 at 01:47 AM 1
Share

(Regarding updated code)

You create a ObjectPool class, which is a good start. However you made it specific to Projectiles. A better design is for it to be generic so you can pool all sorts of things.

Be that as it may, you are creating all the projectiles on startup. They are being positioned and oriented all at once based on the transform of the object that you have the ObjectPool behavior on. This is why they are all on the same position and firing the same direction.

You need to ins$$anonymous$$d initialize the position and rotation of the projectile at the time of activation, and in relation to the gun/player/whatever not in relation to the ObjectPool. Unless, of course, the ObjectPool behavior is on the gun/player. Then it's okay to use the transform, but you still need to do position/orientation at the time the player shoots not at startup.

Show more comments

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

11 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 avatar image

Related Questions

Flip over an object (smooth transition) 3 Answers

[HELP] Rotate Bullet Script 0 Answers

Help regarding World-Local Transfrom Rotation? 1 Answer

Rotation scripts logging error messages? 1 Answer

[Solved] Help on a rotation issue for a Top-Down view? 1 Answer


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