I need help with 2d top-down controller

extends CharacterBody2D
var movement = 500
func _ready():
	pass
func _physics_process(delta):
	var motion = Vector2()
	if Input.is_action_pressed("up"):
		motion.y -= 1
	if Input.is_action_pressed("down"):
		motion.y += 1
	if Input.is_action_pressed("left"):
		motion.x -= 1
	if Input.is_action_pressed("right"):
		motion.x += 1
	motion = motion.normalized()
	motion = move_and_slide(movement*motion) # doesn't work

i need to apply movement and motion variables, but move_and_slide can’t receive any arguments, can you please help me to do small recreation of it?

try it like this:

extends CharacterBody2D
var movement = 500
func _ready():
	pass
func _physics_process(delta):
	var motion = Vector2()
	if Input.is_action_pressed("up"):
		motion.y -= 1 * movement
	if Input.is_action_pressed("down"):
		motion.y += 1 * movement
	if Input.is_action_pressed("left"):
		motion.x -= 1 * movement
	if Input.is_action_pressed("right"):
		motion.x += 1 * movement
	motion = motion.normalized()
	move_and_collide(motion)

This makes it move pretty smooth. If you keep movement * motion inside of the move _and_collide(), the character just zooms around the world crazy fast. and with what I’ve tried with move_and_slide(), it litteraly just dosent work.

One recommendation i’d make is this:
motion.x += 1 * movement * delta
in each of the input if statements.

1 Like

greetings, thank you

1 Like

No problem! glad i could help

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.