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 ekglag2 · Nov 25, 2015 at 08:57 PM · multiple objectspowerupspread

How can I get a multi ball power-up to spread when fired off the paddle in my Arkanoid-like game?

I am working on my final power-up in an Arkanoid type game, the multi ball power-up. I Instantiate three balls at the same transform position as three separate GameObjects:

 multiOne = Instantiate(multiBall, new Vector3 (paddle.paddlePos.x, .902f), transform.rotation) as GameObject;
 multiTwo = Instantiate(multiBall2, new Vector3 (paddle.paddlePos.x, .902f), transform.rotation) as GameObject;
 multiThree = Instantiate(multiBall3, new Vector3 (paddle.paddlePos.x, .902f), transform.rotation) as GameObject;

I then set the boolean "multiActive" to true and run the start function again so that the game waits for a mouse press to fire the balls.

 if (Input.GetMouseButtonDown(0)){
         hasStarted = true;
                     
         if(gameObject.tag == "Faster"){
             this.rigidbody2D.velocity = new Vector2(2f, 15f);
         }else if(multiShoot == true){
             multiOne.rigidbody2D.velocity = new Vector2(-5f, 10f);
             multiTwo.rigidbody2D.velocity = new Vector2(0f, 10f);
             multiThree.rigidbody2D.velocity = new Vector2(5f, 10f);
         }else {
             this.rigidbody2D.velocity = new Vector2(2f, 10f);
         }
 }

The first if statement fires the faster ball at the proper velocity so that it moves quicker than the regular ball but the statement that should make the multi balls go in different directions doesn't seem to activate until the first collision. The balls actually seem to go the velocity of the regular ball (2f, 10f) and when they hit something they all disperse the way I want them to on the mouse press.

Any help would be greatly appreciated! I apologize if this question is posted incorrectly, this is the first time I have posted a question here as I am quite new to Unity.

Here is more of my code as requested. I cut out the methods for the other power ups that I don't think are involved with the issue, just let me know if you want any more code posted.

   void Start () {
          hasStarted = false;
          paddleToBallVector = this.transform.position - paddle.transform.position;
      }
      
      void Update () {
          if (!hasStarted){
              // Lock the ball relative to the paddle
              this.transform.position = paddle.transform.position + paddleToBallVector;
  
              //Wait for mouse press to launch
              if (Input.GetMouseButtonDown(0)){
                  hasStarted = true;
                  
                  if(gameObject.tag == "Faster"){
                      this.rigidbody2D.velocity = new Vector2(2f, 15f);
                  }else if(multiShoot == true){
                      multiOne.rigidbody2D.velocity = new Vector2(-5f, 10f);
                      multiTwo.rigidbody2D.velocity = new Vector2(0f, 10f);
                      multiThree.rigidbody2D.velocity = new Vector2(5f, 10f);
                  }else{
                      this.rigidbody2D.velocity = new Vector2(2f, 10f);
                  }
              }
          }else {
              ballPosX = this.transform.position.x;
              posX3 = posX2;
              posX2 = posX1;
              posX1 = ballPosX;
                  
              if (posX1 == posX2 && posX2 == posX3){
                  StuckBall();
                  Debug.Log("Ball got stuck");
              }
          }
      }
      
      void OnCollisionEnter2D (Collision2D col) {
          Vector2 tweak = new Vector2(Random.Range(0f, 0.2f), Random.Range(0f, 0.2f));
          
          if (hasStarted){
              audio.Play();
              rigidbody2D.velocity += tweak;
          }
      }
      
      void StuckBall(){
              Vector2 unstick = new Vector2(1f, 0f);
              rigidbody2D.velocity += unstick;
      }
  
      public void MultiBall(){
              multiOne = Instantiate(multiBall, new Vector3 (paddle.paddlePos.x, .902f), transform.rotation) as GameObject;
              multiTwo = Instantiate(multiBall2, new Vector3 (paddle.paddlePos.x, .902f), transform.rotation) as GameObject;
              multiThree = Instantiate(multiBall3, new Vector3 (paddle.paddlePos.x, .902f), transform.rotation) as GameObject;
          
          if(powers.randomDown == 2){
              Destroy(faster);
          }else if(powers.randomDown == 3){
              Destroy(ghost);
          }else if(powers.randomDown == 4){
              Destroy(tiny);
          }else if(secondOne == false){
              ball.gameObject.SetActive(false);
          }
          
          secondOne = true;
          multiShoot = true;
          multiActive = 3;
          Start();
          Debug.Log("Multi Ball Active");
      }
      
      public void FasterBall(){
          faster = Instantiate(fasterBall, new Vector3 (paddle.paddlePos.x, .902f), transform.rotation) as GameObject;
          
          if(powers.randomUp == 2){
              Destroy(doubleHit);
          }else if(powers.randomUp == 3){
              Destroy(super);
          }else if(powers.randomUp == 4){
              //Destroy just one of the multi balls, not all three
          }else if(secondOne == false){
              ball.gameObject.SetActive(false);
          }
          
          secondOne = true;
          Start();
      }

Here is the code that calls the MultiBall method you requested.

 public void CaughtPowerUp(){
             AudioSource.PlayClipAtPoint(up, transform.position, 0.8f);
             randomUp = Random.Range(1, 6);
             if(randomUp == 1){
                 paddle.BigPaddle();
             }else if(randomUp == 2){
                 ball.DoubleHit();
             }else if(randomUp == 3){
                 ball.SuperBall();
             }else if(randomUp == 4){
                 ball.MultiBall();
             }else if(randomUp == 5){
                 gem.Barrier();
             }
     }

Comment
Add comment · Show 9
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 ekglag2 · Nov 25, 2015 at 07:45 PM 0
Share

Actually, the balls do not appear to disperse to the velocity I had set. They just disperse.

avatar image Mich_9 · Nov 26, 2015 at 12:22 AM 0
Share

Do you actually are changing the tag "faster" of the gameobject? $$anonymous$$aybe the other statements are being ignored for that reason.

avatar image ekglag2 Mich_9 · Nov 26, 2015 at 04:26 PM 0
Share

I wasn't aware you could even change tags through the script, I thought that had to be done through the inspector. I don't think I am changing it in my script thought. I find objects with the faster tag in the first part of my if statement but that tag is only assigned to the instance of the faster ball power-up and not to the multi balls. That part of my if statement works perfectly, if I instantiate the faster ball power-up the ball is immediately faster than the regular ball when I shoot it.

avatar image ekglag2 · Nov 26, 2015 at 04:47 PM 0
Share

I have deter$$anonymous$$ed that when I fire the multi balls it doesn't seem to activate any section of my if statement but the balls still fire. I added a Debug.Log under each section of the statement and none of the messages appear when I fire the multi balls. I'm not sure how they are firing at all because they should have no velocity if the if statement hasn't set one...

avatar image Mich_9 ekglag2 · Nov 26, 2015 at 05:45 PM 0
Share

Post your complete script, maybe the problem comes from other function or something

avatar image Aggrojag ekglag2 · Nov 30, 2015 at 04:08 AM 0
Share

Show the piece of code that is calling $$anonymous$$ultiBall() please.

avatar image Aggrojag ekglag2 · Nov 30, 2015 at 07:46 PM 0
Share

@ekglag2 I'm just not seeing an issue yet. I've looked over this code twice now, and it looks to be correct. Can you check and see if while playing if the "Faster" tag gets stuck on your ball? Also, how is multiShoot being turned false?

avatar image Mich_9 · Nov 30, 2015 at 07:56 PM 0
Share

A reason cloud be that your ball are colliding with each other at the moment of Instantiation. ¿Are you spawning them in the same spot?

avatar image ekglag2 · Dec 01, 2015 at 02:22 AM 0
Share

@PianoHandsThePerson The faster tag does not get stuck on the ball, it only becomes faster when the appropriate ball is active and the velocity returns to normal when the faster ball gets deactivated. multiShoot is set to false at the start of the code where I first create it. I have the power-ups set to currently last for the duration of the level so I never set the code back to false seeing as how each level reactivates the code from the beginning when a new scene is loaded.

@$$anonymous$$ich_9 I do spawn them at the same location so that very well could be the issue. However, I did think the same thing previously so I set the balls to different layers and set the layers so they couldn't collide with each other. I thought that would solve the issue but ins$$anonymous$$d it caused the balls to all follow the exact same path and never separate at all even when they collide with the walls or gems. When they are all on the same layer they follow the same path until their first collision and then they disperse properly.

0 Replies

· Add your reply
  • Sort: 

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

6 People are following this question.

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

Related Questions

how would i add bullet spread? 0 Answers

How do I make a power up? 2 Answers

PowerUI == simple onclick event 1 Answer

Getting values from multiple objects 1 Answer

How to make an animation proper to a body in multiplayer ? 0 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