Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 impojr · Aug 31, 2017 at 11:14 AM · 2d gamerigidbody2dvelocity

Instantiate new gameobjects on collision, keeping velocity, up to a cap

I'm essentially trying to duplicate Hanzo's scatter arrow from Overwatch, but I'm not having any luck. I have a ScatterArrow Game object and attached a ScatterArrow script that will handle the logic.

I can create more ScatterArrows on collision, but I don't know how to keep the velocity and stop the arrows from spawning after a certain amount of collisions.

For limiting the spawns I've considered an iteration variable, which I increase each collision until it reaches a cap and no more arrows are made.

Here is my code:

 public class ScatterArrow : MonoBehaviour {
 
     public int iteration;
 
     // Use this for initialization
     void Start () {
         
     }
     
     // Update is called once per frame
     void Update () {
         
     }
 
     void OnCollisionEnter2D (Collision2D coll)
     {
         //if the arrows have rebounded less than three times add new scatter arrows
         if (iteration != 3){
             
             GameObject scatterArrow = Instantiate (Resources.Load ("ScatterArrow")) as GameObject; 
 
             //match position of the current arrow
             scatterArrow.transform.position = this.gameObject.transform.position;
 
             //add velocity to the new arrow
 
         }
 
         Destroy (this.gameObject);
     }
 }

  • How would I keep the velocity of the new arrow the same as the old arrow? I have a RigidBody2D attached to the GameObject but I can't access it in code (it's crossed out in the intellisense preview).

  • Is there a way I can make the spawns stop after a few iterations without making another class? Right now increasing the new arrows iteration to +1 takes it from the prefab default of 0, and makes it 1, so it never reaches 3 and continuously respawns in the same position.

Any help would be appreciated, thanks!

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
1

Answer by kashif619 · Sep 01, 2017 at 12:52 AM

As for accessing the rigidbody it is crossed out in intellisense since the variable is deprecated but you can manually cache a reference to rigidbody in the Start(), Something like this.

 Rigidbody2D rb;
 void Start(){ rb = GetComponent<Rigidbody2d>(); }

For keeping the same velocity as this component, you can then use this rb.velocity while Instantiating your new scatter arrow

Lastly for creating exactly 3 arrows this iteration thing seems a bit weird. The simplest way to get 3 arrows is to just Instantiate 3 arrows like this

Or I'm guessing you would need the number of arrows generated configurable then you should have a parameter

 public GameObject arrowPrefab; 
 int numOfArrowsToCreate;

 void OnCollisionEnter2D(){
     //Other stuff

     for(i=0; i<numOfArrowsToCreate; i++)
     {
         GameObject s = Instantiate (arrowPrefab, transform.position, transform.rotation); 
        //You can set each arrows velocity here as well using rb.velocity
      }
 }


To extend this thought you can imagine that the numOfArrowsToCreate might generate random numbers every time they are called. That way you will have random number of scattered arrows just like OverWatch

Comment
Add comment · Show 1 · 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 impojr · Sep 02, 2017 at 04:55 AM 0
Share

Thanks for your reply - the iteration was there because I wanted each recurring scatter arrow to further spawn more arrows - but for now I'll settle with getting the loop you recommended to work.

It does spawn more arrows now, but they overlap and I don't think I'm deleting the original arrow - I can probably fix it by adding a range to the velocity an fixing some stuff - thanks for your help!

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

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

Related Questions

Rigidbody 2D loosing velocity 0 Answers

I want to move the player once while he is jumping 1 Answer

Player doesnt want to move from moving platform 0 Answers

Scaling an object causes slow movement (Since Update 5.5.4) 1 Answer

,Rotate a gameobject around another while being attracted by its gravity 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