![]() ![]() To implement a top-down movement in eight directions, replace your PlayerTopDown node’s script with the following code.īelow, we introduce a function to update our character’s sprite. # `transform.y` stores the node's local axes, allowing us to move it in the direction it's currently facing. # Below, we calculate the forward or backward move direction and directly multiply it to calculate a velocity. ![]() Rotation += rotate_direction * angular_speed * delta get_action_strength( "rotate_right") - Input. # See how we're using Input.get_action_strength() to calculate the direction we rotate. export var angular_speed : = 5.0 func _physics_process(delta): export var speed : = 500 # Rotation speed in radians per second. ![]() extends KinematicBody2D # Movement speed in pixels per second. # Movement where the character rotates and moves forward or backward. #Godot move and slide code#This is why we can directly do subtractions with it.Īttach a script to the PlayerTopDown node with the following code to implement this movement. The function will return 1.0 if you press a key on the keyboard and a value between 0.0 and 1.0 when using a joystick. This function gives us support for analog controllers, like joysticks, allowing the player to move more precisely than with the keyboard. In this tutorial, notice how we use Input.get_action_strength() to calculate the player’s input direction. We want some spaceship that rotates when we press rotate_left or rotate_right, moves forward when we press up, and backward when we press down. Let’s start with the asteroids' movement as it’s the shortest and simplest. Moving like a ship in space, as in Asteroids Remember the different sprites' indices, as you will have to match them with the directions in the code. In the Inspector, add a New SpriteFrames in the Frames property of the AnimatedSprite with all your character’s textures. We use an AnimatedSprite here to change the character’s texture depending on the movement direction. We create a scene with a KinematicBody2D named PlayerTopDown as the root and two children: a CollisionShape2D and an AnimatedSprite. You can review the rest of the actions you have to specify in the following image.įirst, we’ll create a base scene we can use to implement every movement. Add one Key for the right arrow and another one for the D key.įinally, as we want to support analog movement (joysticks), add a Joy Axis to the right. Now, to link events to trigger this action, click on the + button you can find at the end of the field right (the one you have just created). You can see this action has appeared in the list below. To create an action named right open Project -> Project Settings… -> Input Map write in the Action field right and press the Add button. We use input actions to link a specific event (keystrokes, mouse, or joystick interactions) with an action in our game.įor instance, if we want our character to jump when we press the space key, right-click, or the gamepad’s X button, we should link an action to these three events. ![]() Your browser does not support the video tag. #Godot move and slide full#You can find the full source code of the project here.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |