【问题标题】:Basic movement 3D code not working properly基本运动 3D 代码无法正常工作
【发布时间】:2021-06-16 07:30:04
【问题描述】:

我为一个运动角色写了一些代码,只有 W 键(前进)起作用,当我按下 w 和 d 或 a 时,它开始移动但缓慢地向左或向右移动。有人知道如何解决这个问题吗?

  extends KinematicBody
var gravity =Vector3.DOWN * 12
var speed = 4
var jump_speed = 6
var spin = 0.1
var velocity = Vector3()
func _physics_process(delta):
    velocity += gravity * delta
    get_input()
    velocity = move_and_slide(velocity,Vector3.UP)
func get_input():
    velocity.x = 0
    velocity.z = 0
    if Input.is_action_pressed("move_UP"):
        velocity.z -= speed
        if Input.is_action_pressed("move_DOWN"):
            velocity.x -= speed
        if Input.is_action_pressed("strafe_Right"):
            velocity.x -= speed
        if Input.is_action_pressed("strafe_Left"):
            velocity.z -= speed
func _unhandled_input(event):
    if event is  InputEventMouseMotion:
        rotate_y(-lerp(0, spin, event.relative.x/10))
        

【问题讨论】:

    标签: game-engine godot


    【解决方案1】:

    嗯。 Python代码?块由缩进控制。您已经在 move_UP 之后缩进了所有运动条件,因此只有在 move_UP 处于活动状态并首先执行时才能执行它们。取消 move_UP 语句下的三个“If”语句...

    【讨论】:

    • 您的解决方案确实有效,但仅适用于 W 和 A,现在 D 和 S 也可以左右前进。
    • 您确实注意到在“move_UP”和“strafe_Left”(velocity_z -= speed) 以及“move_DOWN”和“strafe_Right”(velocity_x -= speed) 之后发生了相同的操作?
    • 哦,对不起,我没有,我正在学习 3.1 中的教程,我正在使用 3.2,非常感谢。
    • 好吧,现在看来我又回到了原点,我重新排列了 X 和 Z,但它并没有真正做任何事情。
    • 人力资源部。在我看来,strafe_Left 应该与 strafe_Right 相反:一个应该是velocity_x += speed,另一个应该是velocity_x -= speed。同样的道理,如果 move_UP 是 velocity_z -= speed,那么 move_DOWN 应该是 velocity_z += speed。但如果这没有帮助,我想我们会想提出一个新问题。
    猜你喜欢
    • 2014-06-12
    • 1970-01-01
    • 2017-02-28
    • 2017-08-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-16
    相关资源
    最近更新 更多