#Projectile is not directly flying at Enemies (90° wrong direction)

6 messages · Page 1 of 1 (latest)

lyric shoal
#

Hi, I don't know if someone can answer me that without seeing the whole Project but I'm following a tutorial right now, my goal was that a character has a staff, that automatically points at the first entity that got into an Area I have set up with "CollisionShape2D", now the pointing on the enemies with the Staff works fine but my problem is that the bullets always shoot 90° wrong, someone knows maybe why? I tried to find a solution but I don't see my mistake.

(As you can see in the picture the staff is directly pointing at the slimes but the projectile is flying 90° wrong)

Also here is a Video if that helps any further understanding the problem, where I show the problem and some of the things (If that is any useful):
https://www.youtube.com/watch?v=6Twkr9UnVoA

These are the scripts:

For the Staff

extends Area2D

func _physics_process(delta):
    var enemies_in_range = get_overlapping_bodies()
    if enemies_in_range.size() > 0:
        var target_enemy = enemies_in_range.front()
        look_at(target_enemy.global_position)

func shoot():
    const BULLET = preload("res://projectile_fireball.tscn")
    var new_bullet = BULLET.instantiate()
    new_bullet.global_position = %ShootingPoint.global_position
    new_bullet.global_rotation = %ShootingPoint.global_rotation
    %ShootingPoint.add_child(new_bullet)


func _on_timer_timeout():
    %AnimationPlayer.play("Attack")
    shoot()

For the Projectile "Fireball"

extends Area2D

var travelled_distance = 0

func _physics_process(delta):
    %AnimationPlayer.play("default")
    const SPEED = 500
    const RANGE = 750
    
    var direction = Vector2.RIGHT.rotated(rotation)
    position += direction * SPEED * delta
    
    travelled_distance += SPEED * delta
    if travelled_distance > RANGE:
        queue_free()
    


func _on_body_entered(body):
    queue_free()
    if body.has_method("take_damage"):
        body.take_damage()

I also added a picture of the Staff, I did 2 Marker2D's, one for the rotation of the whole staff around the character and one for the Shooting Point. And also added a pic of the scene for the projectile if necessary.

I would appreciate any help! Im kinda lost here.

teal plume
#

If I had to guess I'd say the shooting point's rotation never changes. Does it work if you use the staff's rotation?

new_bullet.global_rotation = global_rotation

If you just want it to work then here is a dirty hack :D

var direction = Vector2.RIGHT.rotated(rotation - deg_to_rad(90))

lyric shoal
#

Wait, now it work again. That is so weird, im confused

#

Okay, so when i start the game. It doesnt start i get the error and then when i press "pause" on the top right it seems to work. Weird...

teal plume
#

weird, try restarting godot