My object doesnt move

Godot Version

4.3

Question

Why does my script not work?

extends CharacterBody2D


const SPEED = 200.0
var mouse_position = null


func _physics_process(delta):
	
	velocity = Vector2(0, 0)
	mouse_position = get_global_mouse_position()
	
	if Input.is_action_just_pressed("forward"):
		var direction = (mouse_position - position).normalized()
		velocity = (direction * SPEED)
		
	move_and_slide()
	look_at(mouse_position)
	

I also tried:

extends CharacterBody2D


const SPEED = 200.0
var velocity = Vector2()
var mouse_position = null


func _physics_process(delta):
	
	velocity = Vector2(0, 0)
	mouse_position = get_global_mouse_position()
	
	if Input.is_action_just_pressed("forward"):
		var direction = (mouse_position - position).normalized()
		velocity = (direction * SPEED)
		
	move_and_slide(velocity)
	look_at(mouse_position)
	

remove the just, make it if Input.is_action_pressed(…)

just???

Change it to Input.is_action_pressed(“forward”)

And also move_and_slide not need parameter, just write move_and_slide(), do not need to include velocity

still doesnt work, at wich script did you look?

remove the just, read my previous replies.

Oh velocity is built-in, you not need to declare it.

so i can just use velocity

1 Like

Correct codes:

extends CharacterBody2D

const SPEED = 200.0
var mouse_position = null

func _physics_process(delta):
	mouse_position = get_global_mouse_position()
	
	if Input.is_action_pressed("forward"):
		var direction = (mouse_position - position).normalized()
		velocity = (direction * SPEED)
		
	move_and_slide()
	look_at(mouse_position)

still doesnt work`extends CharacterBody2D

const SPEED = 200.0
var mouse_position = null

func _physics_process(delta):

velocity = Vector2(0, 0)
mouse_position = get_global_mouse_position()

if Input.is_action_pressed("forward"):
	var direction = (mouse_position - position).normalized()
	velocity = (direction * SPEED)
	
move_and_slide()
look_at(mouse_position)

`

it doesnt work, is it because of my node tree?

What you want and what is the result? how its not working

well i want a spaceship to move to my mouse

so what happens with the codes when you press forward?

nothing.

1 Like

I used the same codes as an experiment, its working. I think something wrong with input map? do you declared the forward as a key?

Is it not moving a bit?

Edited Codes:

extends CharacterBody2D

const SPEED = 200.0
var mouse_position = null

func _physics_process(delta):
	mouse_position = get_global_mouse_position()
	
	velocity = Vector2.ZERO
	
	if Input.is_action_pressed("ui_up"):
		var direction = (mouse_position - position).normalized()
		velocity = (direction * SPEED)
		
	move_and_slide()
	look_at(mouse_position)

press up arrow to move.