0 votes

I've looked around but I can only find tutorials on "guns" or "cannons".

I have a mobile bubble shooter game where KinematicBody2D bubbles are spawned in from the top by basic nodes and they accumulate in order to push themselves down along the screen.

The problem is that I'm trying to make a launcher at the bottom of the screen that spawns one instance of those bubbles at random (of the 6 colors of bubbles) then launches it at the mouse position when left click is released. I've gotten it to spawn one type of bubble, but I can't figure out how to do the rest. It would also need to stop moving once it hits the other bubbles.

Thanks for any help or links in advance.

in Engine by (33 points)
edited by

1 Answer

+1 vote

Problem with godot is that the randi function is not really random hahaha.
here's my random generator function from my one of my projects.

static func randomNumber():
    #Author: Christian Quiel Panuncillo
    #Date: 23/01/2018
    #Description: Returns a random number based on seconds and the randi().
    return OS.get_time().second+randi()

Using this would give you a perfect set of random numbers based on seconds. Convert your code that spawns bubble bullets into a single function argument with at least 1 function argument that accepts bullects, say:

func shootBubble(bullet:PackedScene)

And bullet:PackedScene passed variable is the prefabricated scenes for each type of bubble bullet. Inside the function you can instantiate the bullet as

bullet.instance()

After making the function make an array variable containing all the different bullets. Like:

var magazine = [ load("res://prefabs/yellowBall.tscn"),
                 load("res://prefabs/redBall.tscn"),
                 load("res://prefabs/blueBall.tscn")]

After that call the function as:

shootBubble(magazine[randomNumber()%magazine.size()])

The end result should allow you to randomly select a bubble type to shoot. Hope this helps you.

by (271 points)

As for it to stop moving after collision, I don't know what you have tried or haven't tried so I can can't give any concrete advice there. You can probably use the moveandcollide function and set your collision layer and collision mask to allow it all to collide. You can use moveandcollide to get the object it collided with so use that and add a script that tells the bullet to stop moving when it has detected a collision from a specific type.

I see, I will try this as soon as I get the other stuff working.

Thank you very much!

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.