Created
January 8, 2024 22:23
-
-
Save cfultz/90f8642931dba328426f0aa4bef370bc to your computer and use it in GitHub Desktop.
Godot 4.2.1 Player Script to stop diagonal movement
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extends CharacterBody2D | |
# speed in pixels/sec | |
var speed = 500 | |
func _physics_process(delta): | |
# setup direction of movement | |
var direction = Input.get_vector("left", "right", "up", "down") | |
# stop diagonal movement by listening for input then setting axis to zero | |
if Input.is_action_pressed("right") || Input.is_action_pressed("left"): | |
direction.y = 0 | |
elif Input.is_action_pressed("up") || Input.is_action_pressed("down"): | |
direction.x = 0 | |
else: | |
direction = Vector2.ZERO | |
# normalize the directional movement | |
direction = direction.normalized() | |
# setup the actual movement | |
velocity = (direction * speed) | |
move_and_slide() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment