GDScript 3D Player Movement: Code Example and Explanation
extends KinematicBody\n\nvar move_speed = 10\nvar gravity = -9.8\nvar jump_speed = 15\nvar velocity = Vector3.ZERO\nvar camera: Camera\n\nfunc _ready():\n\tcamera = $Camera\n\nfunc _physics_process(delta):\n\tvar input_vector = Vector3.ZERO\n\n\tif Input.is_action_pressed("move_forward"):\n\t input_vector.z -= 1\n\tif Input.is_action_pressed("move_backward"):\n\t input_vector.z += 1\n\tif Input.is_action_pressed("move_left"):\n\t input_vector.x -= 1\n\tif Input.is_action_pressed("move_right"):\n\t input_vector.x += 1\n\n\tinput_vector = input_vector.normalized()\n\n\tvar camera_transform = camera.transform\n\tvar camera_direction = -camera_transform.basis.z.normalized()\n\tvar camera_right = camera_transform.basis.x.normalized()\n\tvar flat_camera_direction = camera_direction\n\tflat_camera_direction.y = 0\n\tflat_camera_direction = flat_camera_direction.normalized()\n\n\tvar movement_vector = (camera_right * input_vector.x + flat_camera_direction * input_vector.z) * move_speed\n\tmovement_vector.y = velocity.y\n\n\tif is_on_floor() and Input.is_action_just_pressed("jump"):\n\t velocity.y = jump_speed\n\n\tvelocity.y += gravity * delta\n\tmovement_vector += velocity * delta\n\n\tvelocity = move_and_slide(movement_vector, Vector3.UP)
原文地址: http://www.cveoy.top/t/topic/psIT 著作权归作者所有。请勿转载和采集!