运动身体2D不移动

发布于 2025-01-23 03:05:17 字数 557 浏览 0 评论 0原文

我正在尝试使用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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

转身以后 2025-01-30 03:05:17

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 _physics_process(_delta):
    var right = Input.is_action_pressed("move_right")
    var left = Input.is_action_pressed("move_left")
    var jump = Input.is_action_pressed("jump")
    # …

顺便说一下,这些行是布尔值。您可以从0.0获得float 1.0 如果使用input.get_action_strength代替。这也将使您的代码准备好用于模拟输入。


我还想指出,move_and_collide不采用速度,而是位移向量。因此,要正确地调用它,您要乘以delta

func _physics_process(delta):
    # …
    move_and_collide(velocity * delta)

乘以move_and_slide,它确实达到了速度。 顺便说一句,up move_and_slide的参数是辨别地板,天花板和墙壁。没有,一切都被认为是墙。

All this code will run when the KinematicBody2D is initialized:

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")

In consequence, it will not be taking input in real time. Instead you want the last three lines here:

func _physics_process(_delta):
    var right = Input.is_action_pressed("move_right")
    var left = Input.is_action_pressed("move_left")
    var jump = Input.is_action_pressed("jump")
    # …

Those are boolean, by the way. You can get a float from 0.0 to 1.0 if you use Input.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:

func _physics_process(delta):
    # …
    move_and_collide(velocity * delta)

Or use move_and_slide, which does take a velocity. By the way, the up parameter that move_and_slide takes is to discern between floor, ceiling, and walls. Without, everything is considered a wall.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文