• 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
1
Question by lampshade · Oct 13, 2010 at 09:24 PM · instantiation

Instantiating Multiple Projectiles *Collision Detection*

Hi,

Just BEFORE the First Instantiated projectile meets collision you instantiate another one. The second stops as if it has collided with something only because the first has met the collision.

The replied code had some errors: 'Push' and the list template declaration. It also gave me a run-time error for instantiating the Transform type Prefab. I stuck with what I originally had which was GameObject types, so I used a List that accepts that type.

public class GunScript : MonoBehaviour { public GameObject BulletPreFab; public static GameObject instBullet; //public static List<GameObject> Bullet = new List<GameObject>( );

 public void Update()
 {
    if (Input.GetKeyDown("f"))
    {
        instBullet = (GameObject) Instantiate(BulletPreFab, transform.position, Quaternion.identity);
        // Give our Bullet some initial velocity.
        instBullet.rigidbody.velocity = transform.TransformDirection(-Vector3.forward * 255);
        //Bullet.Add(instBullet); 

    }
 }

}

public class CollisionScript : MonoBehaviour { public void OnCollisionEnter (Collider other) { rigidbody.velocity = Vector3.zero; } }

This seems to be the barebones. I am experiencing the same result as the question for this post describes.

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
3

Answer by skovacs1 · Oct 13, 2010 at 10:19 PM

There are several ways to go about this. You would not necessarily need an container of projectiles, but that is one way to go. Another way would be to use a static variable and let the projectiles stop themselves.

Loop through each instance

Make the array public and static so that it is easily accessible and then loop through the array and stop each projectile instance.

GunScript.cs (where you instantiate your projectiles)

using UnityEngine; using System.Collections.Generic;

public class GunScript : MonoBehaviour { public static List<Transform> projectiles = new List<Transform>(); public Transform prefab;

 //..somewhere..
 if(Input.GetButtonUp("Fire1")) {
     Transform shot = Instantiate(prefab, position, rotation) as Transform;
     projectiles.Add(shot);
 }
 //...

}

ColliderScript.cs (on the collider)

using UnityEngine;

public class ColliderScript : MonoBehaviour { public void OnCollisionEnter (Collision collision) { Debug.Log("You hit something");

     if (!audio.isPlaying) {
         audio.clip = CollisionSound;
         audio.Play();
     }

     foreach(Transform shot in GunScript.projectiles)
         shot.rigidbody.velocity = Vector3.zero;
 }

}

Let each instance stop itself

Store an accessible flag common to all projectiles and let them stop themselves, but will involve checking a boolean every frame.

ProjectileScript.cs (on the projectile prefab)

public class ProjectileScript : MonoBehaviour { public static bool allStopped = false; //This will stop them all bool stopped = false; //This will be indicative of each projectile

 public void Update() {
     if(allStopped &amp;&amp; !stopped) {
         rigidbody.velocity = Vector3.zero;
         stopped = true;
     }
 }

}

ColliderScript.cs (on the collider)

public class ColliderScript : MonoBehaviour { public void OnCollisionEnter (Collision collision) { Debug.Log("You hit something");

     if (!audio.isPlaying) {
         audio.clip = CollisionSound;
         audio.Play();
     }

     ProjectileScript.allStopped = true;
 }

}

Comment
Add comment · Show 4 · 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 lampshade · Oct 14, 2010 at 12:34 AM 0
Share

I implemented your code, it all makes total sense, but I am still getting the same result :(

avatar image lampshade · Oct 14, 2010 at 05:07 AM 0
Share

I think the Add is wrong :)

avatar image skovacs1 · Oct 14, 2010 at 02:16 PM 0
Share

The list implementation was an afterthought that I wrote in 5 $$anonymous$$utes after the boolean solution(which I did test). Push should have been Add, some braces were missed, the using shouldn't have had a capital U, System.Collections was missing .Generic, and the instantiate was missing as Transform. It has been corrected and tested.

avatar image lampshade · Oct 14, 2010 at 06:50 PM 0
Share

I was concerned b/c it didn't work. Yes, I realize that with over 2k repuation points the code must have been rushed. I experienced the same results, however, my edited does work, no list, iteration, or boolean needed. Thanks for the help, because it really turned on some lights for me.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

No one has followed this question yet.

Related Questions

Cost of instantiation 2 Answers

IgnorePhysics on BoxCollider at GameObject instantiation 0 Answers

scripts gets disabled when the prefab is instantiated 1 Answer

Generating Objects at High Speeds 0 Answers

Need some help with Random.value and Instantiating objects. 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