Attention | Topic was automatically imported from the old Question2Answer platform. | |
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.)
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)
(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