extends KinematicBody

const GRAVITY = -9.8 const MOVE_SPEED = 10 const JUMP_FORCE = 5 const MOUSE_SENSITIVITY = 0.1

var velocity = Vector3.ZERO var camera_rotation = Vector2.ZERO var camera: Camera

func _ready(): camera = $Camera camera.rotation_degrees.x = 0

Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)

func _physics_process(delta): process_input(delta) process_movement(delta)

func process_input(delta): var mouse_delta = Input.get_mouse_motion() camera_rotation.x -= mouse_delta.y * MOUSE_SENSITIVITY camera_rotation.x = clamp(camera_rotation.x, -90, 90) camera.rotation_degrees.x = camera_rotation.x

var direction = Vector3.ZERO
direction += -camera.global_transform.basis.z * Input.get_action_strength("movement_forward")
direction += camera.global_transform.basis.x * Input.get_action_strength("movement_sideways")
direction = direction.normalized()

if Input.is_action_just_pressed("movement_jump"):
    velocity.y = JUMP_FORCE

velocity.y += GRAVITY * delta

velocity = move_and_slide(velocity, Vector3.UP)

direction = direction.rotated(Vector3.UP, camera_rotation.y)
velocity.x = direction.x * MOVE_SPEED
velocity.z = direction.z * MOVE_SPEED

func process_movement(delta): var movement = velocity * delta movement.y = 0 movement = move_and_slide(movement, Vector3.UP)

if is_on_floor() and velocity.y < 0:
    velocity.y = 0

camera.translation = translation + Vector3(0, 1.5, 0)
translation += movemen
使用GDScript写一段3Dplayer的代码

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

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