extends KinematicBody

const GRAVITY = -9.8 const SPEED = 10 const JUMP_HEIGHT = 5 const JUMP_SPEED = sqrt(2 * JUMP_HEIGHT * -GRAVITY)

var velocity = Vector3.ZERO var isJumping = false

func _physics_process(delta: float) -> void: var input_vector = Vector3.ZERO

if Input.is_action_pressed("move_forward"):
    input_vector.z -= 1
if Input.is_action_pressed("move_backward"):
    input_vector.z += 1
if Input.is_action_pressed("move_left"):
    input_vector.x -= 1
if Input.is_action_pressed("move_right"):
    input_vector.x += 1

if Input.is_action_just_pressed("jump") and is_on_floor():
    velocity.y = JUMP_SPEED
    isJumping = true

input_vector = input_vector.normalized()

velocity.x = input_vector.x * SPEED
velocity.z = input_vector.z * SPEED

velocity.y += GRAVITY * delta

velocity = move_and_slide(velocity, Vector3.UP)

if isJumping and is_on_floor():
    isJumping = false

if Input.is_action_just_pressed("quit"):
    get_tree().quit(
使用GODOT4的GDScript写一段3Dplayer的代码

原文地址: http://www.cveoy.top/t/topic/hJEs 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录