- Home /
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();
}
}
Actually, the balls do not appear to disperse to the velocity I had set. They just disperse.
Do you actually are changing the tag "faster" of the gameobject? $$anonymous$$aybe the other statements are being ignored for that reason.
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.
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...
Post your complete script, maybe the problem comes from other function or something
Show the piece of code that is calling $$anonymous$$ultiBall() please.
@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?
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?
@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.
Your answer
Follow this Question
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