How to get (bullet-)spawn positions with a rotated Vector2?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Czselu349

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://forum.godotengine.org/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)

can u show the code of the bullet?

whiteshampoo | 2020-06-30 13:14

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()

Czselu349 | 2020-06-30 17:26

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

whiteshampoo | 2020-06-30 18:29

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.)

Czselu349 | 2020-06-30 19:31

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

whiteshampoo | 2020-06-30 20:28

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

Czselu349 | 2020-06-30 21:03

:bust_in_silhouette: Reply From: whiteshampoo

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

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)

Thanks! I see you’re really a part of the community :slight_smile:

But can you resend the link please? It says:
#“error 404 This XML file does not appear to have any style information associated with it. The document tree is shown below.”

Czselu349 | 2020-07-01 08:45

because i’m dumb. I accidentially used a “1 download”-filehoster xD
I’l upload it when i’m back on the PC with the project… (in around 30-60 minutes)

whiteshampoo | 2020-07-01 09:22

please try again

whiteshampoo | 2020-07-01 10:12

OK thank you!

Czselu349 | 2020-07-01 13:36

I’ve been trying to open it for half an hour now, and every time I open the project the screen becomes black, and it doesn’t respond to anything except for the restart button. :frowning: I don’t know what could cause it. I don’t have a strong PC, but it doesn’t even let me go into the editor.

Czselu349 | 2020-07-01 14:12

I’ve been trying to open it for half an hour now, and every time I open the project the screen becomes black, and it doesn’t respond to anything except for the restart button. :frowning: I don’t know what could cause it. I don’t have a strong PC, but it doesn’t even let me go into the editor.

Okay, I’ve put the files into a differrent project’s folder, selected the main scene, and it freaking worked!! I’m so happy right now :slight_smile:

Czselu349 | 2020-07-01 14:24

mh, i tried it on a different PC, and it worked out of the box… strange ^^
but if it works now, then it’s fine xD

Hope you can learn something from it :wink:

whiteshampoo | 2020-07-01 14:26

I am learning, a lot… XD
I like the line # self explanatory (That was why the enemy didn’t know how to aim)

I wasted like 2 entire weeks not knowing what to do, and it turned out to be this simple.
At this point I can only laugh. XD

Czselu349 | 2020-07-01 14:42

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…

whiteshampoo | 2020-07-01 14:50

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)

Czselu349 | 2020-07-01 16:28

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)

whiteshampoo | 2020-07-01 16:40

What’s your discord name?? I’m called “unnamed_yet”

Czselu349 | 2020-07-01 16:43

whiteshampoo#0102

whiteshampoo | 2020-07-01 16:56