英文:
KinematicBody2D not moving at all when testing game
问题
我第一次使用Godot,但出现了一个问题,玩家角色完全不移动,但它确实可以检测到输入。以下是我编写的代码(GDScript):
extends KinematicBody2D
const UP = Vector2(0, -1)
const GRAVITY = 20
const MAXFALLSPEED = 200
const ACCELERATION = 30
const MAXSPEED = 100
var motion = Vector2()
func _ready():
pass
func _physics_process(delta):
motion.y += GRAVITY
if motion.y > MAXFALLSPEED:
motion.y = MAXFALLSPEED
if Input.is_action_pressed("right"):
motion.x += ACCELERATION
elif Input.is_action_pressed("left"):
motion.x -= ACCELERATION
else:
motion.x = 0
当我测试游戏时,什么都不发生。
英文:
I'm using godot for the first time, and for some reason the player does not move at all, however it does detect the inputs.
This is the code I wrote (gdscript)
extends KinematicBody2D
const UP = Vector2(0,-1)
const GRAVITY = 20
const MAXFALLSPEED = 200
const ACCELERATION = 30
const MAXSPEED = 100
var motion = Vector2()
func _ready():
pass
func _physics_process(delta):
motion.y += GRAVITY
if motion.y > MAXFALLSPEED:
motion.y = MAXFALLSPEED
if Input.is_action_pressed("right"):
motion.x += ACCELERATION
elif Input.is_action_pressed("left"):
motion.x -= ACCELERATION
else:
motion.x = 0
When I test the game, nothing happens at all
答案1
得分: 1
你忘了添加move_and_slide()
,朋友
extends KinematicBody2D
const UP = Vector2(0,-1)
const GRAVITY = 20
const MAXFALLSPEED = 200
const ACCELERATION = 30
const MAXSPEED = 100
var motion = Vector2()
func _ready():
pass
func _physics_process(delta):
motion.y += GRAVITY
if motion.y > MAXFALLSPEED:
motion.y = MAXFALLSPEED
# 我的项目设置为"ui_right"和"ui_left",而不是"right"和"left"
if Input.is_action_pressed("ui_right"):
motion.x += ACCELERATION
elif Input.is_action_pressed("ui_left"):
motion.x -= ACCELERATION
else:
motion.x = 0
move_and_slide(motion) # 你漏掉了这一部分,伙计!
Also, It's dangerous to go alone! Take this: https://docs.godotengine.org/en/stable/tutorials/physics/using_kinematic_body_2d.html
Or if you're a lazy reader like me:
https://www.youtube.com/watch?v=Ge0RiUx_NzU
英文:
You forgot to add move_and_slide()
buckaroo
extends KinematicBody2D
const UP = Vector2(0,-1)
const GRAVITY = 20
const MAXFALLSPEED = 200
const ACCELERATION = 30
const MAXSPEED = 100
var motion = Vector2()
func _ready():
pass
func _physics_process(delta):
motion.y += GRAVITY
if motion.y > MAXFALLSPEED:
motion.y = MAXFALLSPEED
# My project settings has "ui_right" & "ui_left" instead of "right" and "left"
if Input.is_action_pressed("ui_right"):
motion.x += ACCELERATION
elif Input.is_action_pressed("ui_left"):
motion.x -= ACCELERATION
else:
motion.x = 0
move_and_slide(motion) #You missed this chad!
Also, It's dangerous to go alone! Take this: https://docs.godotengine.org/en/stable/tutorials/physics/using_kinematic_body_2d.html
Or if you're a lazy reader like me:
https://www.youtube.com/watch?v=Ge0RiUx_NzU
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论