Trouble with rotated()

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

I made top down shooter and shooting on arrows, but bullets don’t turn in the right direction

extends KinematicBody2D
export var bullet_speed = 500
export var fire_rate = 0.2

var bullet = preload("res://Bullet.tscn")
var can_fire = true
var velocity = Vector2.ZERO

func _process(delta):
    var bullet_vector = Vector2.ZERO
    var bullet_instance = bullet.instance()
    bullet_instance.position = global_position

    bullet_vector.x = Input.get_action_strength("ui_right") - Input.get_action_strength("ui_left")
    bullet_vector.y = Input.get_action_strength("ui_down") - Input.get_action_strength("ui_up")
    bullet_vector = bullet_vector.normalized()

    if bullet_vector != Vector2.ZERO and can_fire:
	    bullet_instance.apply_impulse(bullet_vector, bullet_vector.rotated(rotation) * bullet_speed)
	    can_fire = false
	    yield(get_tree().create_timer(fire_rate), "timeout")
	    can_fire = true

func _physics_process(delta):
    var input_vector = Vector2.ZERO
    input_vector.x = Input.get_action_strength("move_right") - Input.get_action_strength("move_left")
    input_vector.y = Input.get_action_strength("move_down") - Input.get_action_strength("move_up")
    input_vector = input_vector.normalized()

    if input_vector != Vector2.ZERO:
	    velocity = velocity.move_toward(input_vector * 200, 100)
	    velocity = velocity.move_toward(Vector2.ZERO, 50)

    velocity = move_and_slide(velocity)

The position of bullet should not set to global, because you cannot make sure your node parent is the root, although this cannot solve your question but it can help you maintenance your project

suyen729 | 2020-05-10 07:34

:bust_in_silhouette: Reply From: suyen729

The offset of apply_impulse is wrong, it will make bullet rotate, you should use apply_central_impulse(bullet_vector.rotated(rotation) * bullet_speed)