0 votes

Hello! It's my first time posting in this forum, so sorry if I muck anything up.

I've recently started learning GDScript about a month ago, and aside from GameMaker 2's language, its the first language I've really gone in depth to try and learn.

While there are very few tutorial channels out there, but those that make videos are very dedicated, however I haven't found a tutorial video for specifically what I'd like to do:

Conceptually it's fairly simple, I've created a character and a gun sprite and have coded the gun sprite to face towards the mouse at all times. What I want to do now is make it so that when you click the mouse button, it fires a bullet towards wherever the mouse is.

For whatever reason... I just cannot figure this out. Any help, however simple, would be hugely appreciated. Thank you.

in Engine by (12 points)

Rather than look for a tutorial that shows exactly what you want to do, you should strive to understand the concepts - then you can make anything you need.

Firing a bullet towards the mouse can be broken down into two (and a half) pieces:

1) Find the direction vector from the gun to the mouse.
2) Instance a bullet.
2a) Point the bullet in the given direction and let it go.

It's hard to answer your question because you may actually be asking multiple questions.
It seems like you've done #1 since you can point the gun towards the mouse. For #2, have you made a bullet scene? If so, how have you set it up? With a properly set up bullet, #2a should be as easy as passing it the direction from #1.

There's some example code in the docs here for one approach to the problem:

Hi! Thank you very much for replying. I didn't expect such a quick response, especially from such a big Godot channel.

Thanks a ton for the link to the instancing example. I followed it, and seem to keep getting the following error: Invalid get index "velocity". The game freezes and won't let me interact with it when this happens.. it only happens when I press the mouse button to fire the bullet. Any suggestions as to why I can't access the velocity value?

Did you define a velocity variable in your bullet's script?

Hello again, I did define a velocity variable. I followed the code exactly, and the current issue is that instead of the bullets firing towards my mouse, they simply appear at my cursors location.

Here is my player code:

extends Sprite

signal shoot(bullet, direction, location)

var Bullet = preload("res://Bullet.tscn")

func _input(event):
    if event is InputEventMouseButton:
        if event.button_index == BUTTON_LEFT and event.pressed:
            emit_signal("shoot", Bullet, rotation, position)

func _process(delta):

func _on_Sprite_shoot(bullet, direction, location):
    var b = Bullet.instance()
    b.rotation = direction
    b.position = location
    b.velocity = b.velocity.rotated(direction)

And in the bullet:

extends Area2D

var velocity = Vector2.ZERO

func _physics_process(delta):
    position += velocity * delta

Really sorry for all the questions, thank you for the help!

In that example, the "shoot" signal was supposed to be connected to your "Main" scene, which is where the _on_Sprite_shoot() should be. Your code is making the bullet the child of the player, so its position isn't going to be at the player, but offset by whatever position is at the time.

You don't have to use a signal. You can just do get_tree().get_root().add_child(b). But you want the bullet's global_position to be placed at the players position.

Thanks again, my mistake on that one. It is now creating the bullet at the players location, but the bullet just sits there.

My code is the same except the "Shoot" signal is in my main scene node:

extends Node

var Bullet = preload("res://Bullet.tscn")

func onPlayershoot(bullet, direction, location):
var b = Bullet.instance()
b.rotation = direction
b.position = location
b.velocity = b.velocity.rotated(direction)

I'm sure it seems like I'm floundering around with something simple, but I appreciate the help

1 Answer

0 votes

Welcome! I'm also a former GM:S user :)

Is this your bullet code?

extends Area2D

var velocity = Vector2.ZERO

func _physics_process(delta):
    position += velocity * delta

Notice the velocity vector. It's set to zero and never updated (delta times the zero vector equals the zero vector) so the bullets stay still. Change it so that the velocity is being updated (also consider using normalize() and a separate constant for frame-independent animation. Something like this:

var direction: Vector2
var speed: int = 10

func _process(delta):
    position += direction.normalize()  * speed * delta

Also, that's a bit unrelated, but I noticed you use look_at(get_global_mouse_position()) to get your mouse's position. There is a cleaner way to do this, which needs a bit of a readup about the Input workflow on https://docs.godotengine.org/en/3.1/tutorials/inputs/mouse_and_input_coordinates.html, and especially on the part regarding InputEventMouseMotion.

Good luck!

by (19 points)
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.