extends KinematicBody

export var speed = 10 var velocity = Vector3.ZERO var gravity = -9.8 var jumpHeight = 5 var isJumping = false var raycast = RayCast.new() var raycastDistance = 1 var raycastNormal = Vector3.ZERO

func _ready(): raycast = get_node('RayCast') set_physics_process(true)

func _physics_process(delta): var moveDirection = Vector3.ZERO moveDirection.x = Input.get_action_strength('move_right') - Input.get_action_strength('move_left') moveDirection.z = Input.get_action_strength('move_forward') - Input.get_action_strength('move_backward')

moveDirection = moveDirection.normalized()
moveDirection = moveDirection.rotated(Vector3.UP, deg2rad(-45))

velocity.y += gravity * delta
if is_on_floor():
    velocity.y = 0
    if Input.is_action_just_pressed('jump'):
        velocity.y = sqrt(jumpHeight * -2 * gravity)

velocity = move_and_slide_with_snap(moveDirection * speed, Vector3.UP, Vector3.ZERO, true, 4, deg2rad(45))

if raycast.is_colliding():
    raycastNormal = raycast.get_collision_normal()
    if raycastNormal.y > 0.7:
        isJumping = false
    else:
        isJumping = true
else:
    isJumping = true

if isJumping:
    velocity.y += gravity * delta
else:
    velocity.y = 0

velocity = move_and_slide(velocity, Vector3.UP)

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

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