3D 游戏玩家代码示例:使用 GDScript
extends KinematicBody\n\nconst GRAVITY = -9.8\nconst MOVE_SPEED = 10\nconst JUMP_FORCE = 5\nconst MOUSE_SENSITIVITY = 0.1\n\nvar velocity = Vector3.ZERO\nvar camera_rotation = Vector2.ZERO\nvar camera: Camera\n\nfunc _ready():\n\tcamera = $Camera\n camera.rotation_degrees.x = 0\n\tInput.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)\n\nfunc _physics_process(delta):\n process_input(delta)\n process_movement(delta)\n\nfunc process_input(delta):\n var mouse_delta = Input.get_mouse_motion()\n camera_rotation.x -= mouse_delta.y * MOUSE_SENSITIVITY\n camera_rotation.x = clamp(camera_rotation.x, -90, 90)\n camera.rotation_degrees.x = camera_rotation.x\n\tvar direction = Vector3.ZERO\n direction += -camera.global_transform.basis.z * Input.get_action_strength("movement_forward")\n direction += camera.global_transform.basis.x * Input.get_action_strength("movement_sideways")\n direction = direction.normalized()\n\tif Input.is_action_just_pressed("movement_jump"):\n velocity.y = JUMP_FORCE\n\tvelocity.y += GRAVITY * delta\n\tvelocity = move_and_slide(velocity, Vector3.UP)\n\tdirection = direction.rotated(Vector3.UP, camera_rotation.y)\n velocity.x = direction.x * MOVE_SPEED\n velocity.z = direction.z * MOVE_SPEED\n\nfunc process_movement(delta):\n var movement = velocity * delta\n movement.y = 0\n movement = move_and_slide(movement, Vector3.UP)\n\tif is_on_floor() and velocity.y < 0:\n velocity.y = 0\n\n camera.translation = translation + Vector3(0, 1.5, 0)\n translation += movement
原文地址: http://www.cveoy.top/t/topic/psIC 著作权归作者所有。请勿转载和采集!