0 votes

My gun already aims at the mouse, so i just need to shoot bullets, and i tried a lot of tutorials, but nothing worked. I am a beginner, and i still can't code on my own. The character moves and the gun is attached to the character and it (the gun) rotates to the cursor position.

Godot version Godot 3.2.3 stable win64
in Engine by (24 points)

What do you mean nothing worked? Have you tried preloading the bullet scene and then instancing it?

There was a tutorial of this type and the first steps worked but in the end it didn’t work

Can I see your code

2 Answers

+1 vote
Best answer

https://youtu.be/cei9BZMzVLY
This video is very good and you'll learn a lot. I reckon you want to achieve something similar to this

by (413 points)
selected by

Thank you so much! It worked!

+1 vote

First you will need to decide what type of node you will use for the bullets.

Mainly you will choose from RayCast2D or RigidBody2D (or KinematicBody2D), they both have their advantages and disadvantages but basically:

For bullets that are very fast (think realistic levels of bullet speed) i would use a RayCast2D, this will be a "hitscan" bullet (This means the hit is registered where the raycast is colliding at the exact frame you shoot, there is nothing travelling)

For something like, lets say, a missile or a cannon shot, i would use RigidBody2D this would be a "projectile" (This means it is a physical body "launched" from the weapon and it travels across the map in the physical space) IMPORTANT: if you use this method and the projectile travels too fast, it may pass through objects, since it will be in front of the object in one frame and then have passed it in the next frame, there will be no frame in which the projectile and the object overlap, and therefore no collision detected.

For a RayCast2D, you would first create a raycast2D node as a child of the weapon (So it inherits its direction) and that's pretty much it (Besides setting up the length of the "Cast_to" property, depending on how far you want the shot to travel)

With a RigidBody2D it is a bit more tricky, you will have to create the projectile in a different scene and then create and instance of it when you shoot. After that you would apply_central_impulse

Keep trying, you will figure it out, try to start small to get the hang of it and dont forget to google the terms i used here. Good luck!

by (213 points)

I tried a lot of tutorials, and almost all of them were with Area2D, and from like 12 tutorials in just 1 the bullets showed but they didn't shoot from the right position.

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 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 webmaster@godotengine.org with your username.