if i try to shoot my bullet it gets created and everthing but it wont go in the direction it is supposed to be.

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

This is the code i use and the Error Message: Invalid set index velocity (on base: Node2D) with value of type Vector2:

extends KinematicBody2D

var velocity = Vector2(0,0)
const bulletPath = preload(“res://Bullet.tscn”)

func _physics_process(delta):

$Node2D.look_at(get_global_mouse_position())

if Input.is_action_pressed("ui_left"):
	velocity.x = -100

if Input.is_action_pressed("ui_right"):
	velocity.x = 100

if Input.is_action_pressed("ui_up"):
	velocity.y = -100

if Input.is_action_pressed("ui_down"):
	velocity.y = 100

if Input.is_action_just_pressed("ui_shoot"):
	_shoot()

move_and_slide(velocity)
velocity.x = lerp(velocity.x,0,0.3)
velocity.y = lerp(velocity.y,0,0.3)

func _shoot():
var bullet = bulletPath.instance()
get_parent().add_child(bullet)
bullet.position = $Node2D/Position2D.global_position
bullet.velocity = get_global_mouse_position() - bullet.position

Thanks for your help <3

:bust_in_silhouette: Reply From: horsecar123

Invalid set index velocity (on base: Node2D) means your bullet doesnt have velocity variable . You should change your bullets node type from node2D to kinematic body or add a velocity variable to your bullet script and move it with that velocity variable