Attention | Topic was automatically imported from the old Question2Answer platform. | |
Asked By | Rawayju |
So, I was following this tutorial https://www.youtube.com/watch?v=BeSJgUTLmk0 by HeartBeast and I copy pasted the code by hand, the only different things are the variable names. For some reason, it doesn’t work… this is the code
extends KinematicBody2D
var limit_speed: float = 500
var acceleration: float = 2000
var motion = Vector2.ZERO
func _physics_process(delta):
var axis = get_input_axis()
if axis == Vector2.ZERO:
apply_friction(acceleration * delta)
else:
apply_movement(axis * acceleration * delta)
motion = move_and_slide(motion)
func get_input_axis():
var axis = Vector2.ZERO
axis.x = int(Input.is_action_pressed("ui_right")) - int(Input.is_action_pressed("ui_left"))
axis.y = int(Input.is_action_pressed("ui_down")) - int(Input.is_action_pressed("ui_up"))
return axis.normalized()
func apply_friction(amount):
if motion.length() > amount:
motion -= motion.normalized() * amount
else:
motion = Vector2.ZERO
func apply_movement(acceleration):
motion += acceleration
motion = motion.clamped(limit_speed)
Just by writing that down, I guess it was supposed to move when I pressed WASD or the direction keys, but nothing happens, any idea why? How is it supposed to know that I’m pressing WASD if I never coded the controls? I guess I didn’t understand a part of the video xD
screenshot of the code: Screenshot by Lightshot sorry for not knowing how to use ctrl + k :d
Edited to fix your formatting. Please format code samples.
kidscancode | 2020-05-02 23:25
you need to setup the INPUT KEY in your project settings.
Go to project>Setting>Input
And then addd one input per key matching the name that was given to you in the script.
Good luck !
quizzcode | 2020-05-06 20:46