![]() |
Attention | Topic was automatically imported from the old Question2Answer platform. |
![]() |
Asked By | malthus |
I was just following a simple tutorial. When I instantiate my Bullets, the apply_impulse won’t work.
Here’s the whole script:
extends CharacterBody2D
var movespeed = 500
var bullet_speed = 2000
var bullet = load(“res://bullet.tscn”)
func _ready():
pass
func _physics_process(delta):
velocity.x = 0
velocity.y = 0
if Input.is_action_pressed(“up”):
velocity.y -= 1
if Input.is_action_pressed(“down”):
velocity.y += 1
if Input.is_action_pressed(“left”):
velocity.x -= 1
if Input.is_action_pressed(“right”):
velocity.x += 1
velocity = velocity.normalized() * movespeed
move_and_slide()
look_at(get_global_mouse_position())
if Input.is_action_just_pressed("LMB"):
fire()
func fire():
var bullet_instance = bullet.instantiate()
bullet_instance.position = get_global_position()
bullet_instance.rotation_degrees = rotation_degrees
bullet_instance.apply_impulse(Vector2(),Vector2(bullet_speed,0).rotated(rotation_degrees))
get_tree().get_root().call_deferred(“add_child”,bullet_instance)