0 votes

Hi!
What I want to achieve is something like this: (There's only 3 bullets, because it was quicker to draw, And because I want to be able to change the number of bullets shoted at once.)
As you can see, the middle is aiming to the player
Currently, it doesn't aims to the player well, and I don't know how to fix that. It always aims off. The aiming isn't just off a certain value, when I go around The enemy, sometimes it shoots to the opposite direction, sometimes it's off by something like 45 degrees, (45 is just an example) and sometimes, it even shoots correctly.
Thecnical stuff: (My code is at the bottom)
I'm rotating a vector2 (radius) and spawn the bullets at it's position. I'm making a regular node (center) lookat() the player, and add it's rotation to the vector2 (radius)
(The player and the enemy are KinematicBody2D-s and the bullets are Area2D-s wich have their globalposition updated.)
I'm using a lot of ideas from this queastion'S 2nd aswer:
https://godotengine.org/qa/12826/spawning-objects-around-a-circle

This is how it aims: (The flame thingy shows the rotation of the center (Node2D),
but as you can see, the bullets are not spawned at a correct position)
enter image description here

(If you need any more information, just say it, because I don't know what else to say.)
BTW the visuals will be worked on after I will fix the main BUGs in the game. (just like this one)

Also here's the whole project if you need it: https://github.com/TeoTheOO/Project-BUG

(You can move with WASD and the ARROWS and you can throw the sword and recall it with mouse button 1 (left))

export(int) var shoot_angle = 90  # in degrees
export(int) var numb_of_bullets = 3
export(float) var time_between_shots = 0.5
var radius = Vector2(100, 0)
onready var center = get_node("Center")

func _ready():
 shoot_angle = deg2rad(shoot_andgle)

var angle_step = shoot_angle / numb_of_bullets

center.look_at(player_node.global_position)

for i in range(numb_of_bullets):
  var spawn_pos = center.global_position + radius.rotated((center.global_rotation / 2) + angle_step * i) # * sign(center.global_rotation / 2))

var node = bullet.instance()
node.global_position = spawn_pos
get_parent().add_child(node)
node.move_direction = global_position.direction_to(node.global_position)
asked Jun 30 in Engine by Czselu349 (139 points)
edited Jun 30 by Czselu349

can u show the code of the bullet?

Of course!

Here's the code for the bullet:

(as you can see, I'm giving the move direction to the bullet, when I spawn It.)

extends Area2D

# Varaiables for moveing and deleting
var move_direction = Vector2(0,0)
export(int) var speed = 10
export(float) var live_time = 5 # in seconds
# Damage
export(int) var dmg = 10

func _ready():
 $Timer.start(live_time)

func _physics_process(delta):
if move_direction != Vector2(0,0):
    global_position += move_direction * speed

func _on_Timer_timeout():
 destroy_bullet()

func _on_Area2D_area_entered(area):
if area.is_in_group("PlayerHitbox"):
    area.get_parent().take_damage(dmg)
    destroy_bullet()
if area.is_in_group("SwordHitbox"):
    destroy_bullet()

func destroy_bullet():
 queue_free()

try this:

spawn_pos = center.global_position +radius.rotated(center.rotation + deg2rad(-shoot_angle / numb_of_bullets * 2.0 + angle_step * i))

dont forget to multiply with delta in your bullet script, or it will have different speeds on different PCs

Thanks, for the tip! I'd be glad to say, that the code actually worked, but it didn't.

Also, I might have forget, that the aiming isn't just off a certain value, when I go around The enemy, sometimes it shoots to the opposite direction, sometimes it's off by 45 degrees, and sometimes, it even shoots correctly. And I didn't forget to make the shoot_angle varaiable deg2rad(), I just did this in the ready function.
(So now I'll go and edit the question.)

i'll make an example-project tomorrow.
It's hard if i/we don't know how your node-tree looks and what scripts look like.
I hope i can discipline myself enough to make enough comments xD

Thank you for the efforts! XD
Also, I went ahead and created a GitHub respository, so you can go, and see everything you need. https://github.com/TeoTheOO/Project-BUG

1 Answer

+1 vote
Best answer

I made an example project for you.
I hope it will help you (and maybe others)!

https://workupload.com/file/MbUc9bwbXaY

Use any mouse-button to let them shoot at the mouse-position.
The mouse-wheel can make a very pretty bullet-hell-effect xD

I put a decent amount of love into this, so please read and try to understand everything in there. (it's not that much script...)
If you have any questions, feel free to ask! (Here, Discord or Reddit)

EDIT:
updated URL

(Made with 3.2.2 but should work with 3.2.1, too)

answered Jul 1 by whiteshampoo (976 points)
selected Jul 1 by Czselu349

i know that feeling just too well...

i also was stuck at the aiming with multiple projectiles for some minutes. I just can't figure out a nice way to make this happen in just 1 formula.
Then i was annoyed and just handled 1 projectile by itself...

It's fine, I'm just making my first (hopefully) complete game, so I'm planning on it being small. The goal of this project is to see what I can do by "myself". After that I'll see what a (payed) tutorial could teach me. (even if that doesn't sound that smart) I only have like 2 BUGs to go and after that only art and level design is needed for my game to be complete. You think you can help in those too? (Only if you have the time though)

If you have trouble in finishing anything (like me...) you can try game-jams. There you have a fixed amount of time to make a game that matches a given theme.
I can recommend the "Godot Wild Jam" on itch.io

Show me dat bugs! (New thread or somewhere else)

What's your discord name?? I'm called "unnamed_yet"

whiteshampoo#0102

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.