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)
KingGD
2
remove the just, make it if Input.is_action_pressed(…)
KingGD
4
Change it to Input.is_action_pressed(“forward”)
KingGD
5
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?
KingGD
8
remove the just, read my previous replies.
KingGD
11
Oh velocity is built-in, you not need to declare it.
so i can just use velocity
1 Like
KingGD
13
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?
KingGD
16
What you want and what is the result? how its not working
well i want a spaceship to move to my mouse
KingGD
18
so what happens with the codes when you press forward?
KingGD
21
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?
KingGD
23
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.