0 votes

Please help me to make this
That is code:
`extends KinematicBody2D

const ACCELERATION = 750
const MAX_SPEED = 50
const FRICTION = 500

var velocity = Vector2.ZERO

onready var animationPlayer = $AnimationPlayer
onready var animationTree = $AnimationTree

func physicsprocess(delta)
var inputvector = Vector2.ZERO
input
vector.x = Input.getactionstrength(uiright) - Input.getactionstrength(uileft)
inputvector.y = Input.getactionstrength(uidown) - Input.getactionstrength(uiup)
input
vector = input_vector.normalized()

if input_vector != Vector2.ZERO
    animationTree.set(parametersIdleblend_position, input_vector)
    velocity = velocity.move_toward(input_vector  MAX_SPEED, ACCELERATION  delta)
else
    velocity = velocity.move_toward(Vector2.ZERO, FRICTION  delta)

velocity = move_and_slide(velocity)`
Godot version latest
in Projects by (24 points)

1 Answer

+1 vote
Best answer

The key to 4 directional movement is using an elif for each direction, so you can only move in one direction at a time, and not 2 such as right and down. You don't have to copy this code exactly, but I think it will give you a good start.

extends KinematicBody2D

export (int) var speed = 200

var velocity = Vector2()

func get_input():
    velocity = Vector2()
    if Input.is_action_pressed('right'):
        velocity.x += 1
    elif Input.is_action_pressed('left'):
        velocity.x -= 1
    elif Input.is_action_pressed('down'):
        velocity.y += 1
    elif Input.is_action_pressed('up'):
        velocity.y -= 1
    velocity = velocity.normalized() * speed

func _physics_process(delta):
    get_input()
    velocity = move_and_slide(velocity)

Btw this is the official Godot 8 directional movement code that I added elif to so it would be 4 directions.

by (446 points)
selected by

On little question, how can i add my animation then?

First you would make an onready variable for the animation player.Then either in a new function that is called in the physics process, or just directly in the physics process, you would have to figure out which animation to play. I have no idea what your animations are though. If you are using an animation player and 4 direction animations, this video should greatly help:

https://www.youtube.com/watch?v=M4wuhFgubWY

I hope this helps! Animations are a very broad topic though, if you need help on something more specific I can.

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.

Categories