KinematicBody2D 在测试游戏时根本不移动

huangapple go评论64阅读模式
英文:

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

huangapple
  • 本文由 发表于 2023年2月24日 02:05:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/75548662.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定