运动身体2D不移动
我正在尝试使用Godot Engine玩游戏,但我一开始就卡住了!我无法使我的运动学2D移动!
这是我的播放器。gd脚本
extends KinematicBody2D
var velocity = Vector2.ZERO
var move_speed = 480
var gravity = 1200
var jump_force = -720
var right = Input.is_action_pressed("move_right")
var left = Input.is_action_pressed("move_left")
var jump = Input.is_action_pressed("jump")
func _ready():
pass
func _physics_process(_delta):
var move_direction = int(right) - int(left)
velocity.x = move_speed * move_direction
move_and_collide(velocity)
,请有人帮我吗?
I'm trying to game a game using the Godot Engine but I'm stuck at the beginning! I can't make my KinematicBody2D move!
This is my Player.GD script
extends KinematicBody2D
var velocity = Vector2.ZERO
var move_speed = 480
var gravity = 1200
var jump_force = -720
var right = Input.is_action_pressed("move_right")
var left = Input.is_action_pressed("move_left")
var jump = Input.is_action_pressed("jump")
func _ready():
pass
func _physics_process(_delta):
var move_direction = int(right) - int(left)
velocity.x = move_speed * move_direction
move_and_collide(velocity)
Can someone, please, help me?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当
Kinematicbody2d
初始化时,所有这些代码将运行:因此,它不会实时输入。取而代之的是,您想要最后三行:
顺便说一下,这些行是布尔值。您可以从
0.0
获得float
1.0 如果使用input.get_action_strength
代替。这也将使您的代码准备好用于模拟输入。我还想指出,
move_and_collide
不采用速度,而是位移向量。因此,要正确地调用它,您要乘以delta乘以
move_and_slide
,它确实达到了速度。 顺便说一句,up
move_and_slide
的参数是辨别地板,天花板和墙壁。没有,一切都被认为是墙。All this code will run when the
KinematicBody2D
is initialized:In consequence, it will not be taking input in real time. Instead you want the last three lines here:
Those are boolean, by the way. You can get a
float
from0.0
to1.0
if you useInput.get_action_strength
instead. Which will also let your code ready for analog input.I also want to point out that
move_and_collide
does not take a velocity, but a displacement vector. So to call it correctly, you want to multiply the velocity by delta:Or use
move_and_slide
, which does take a velocity. By the way, theup
parameter thatmove_and_slide
takes is to discern between floor, ceiling, and walls. Without, everything is considered a wall.