extends CharacterBody3D const SPEED = 5.0 const JUMP_FORCE = 4.5 # Get the gravity from the project settings to be synced with RigidDynamicBody nodes. var gravity = ProjectSettings.get_setting("physics/3d/default_gravity") var starting_point = Vector2(DisplayServer.window_get_size().x / 2, DisplayServer.window_get_size().y / 2) var camera_rotate_y = 0.0 var camera_rotate_x = 0.0 var player_rotate_y = 0.0 var speed_rotate_1sec = PI var max_angle = PI / 2 const TWO_PI = 2.0 * PI const PI_2 = PI / 2.0 func _init(): pass func _ready(): # Place the mouse at the center of the screen get_viewport().warp_mouse(starting_point) func _input(event): # If right mouse button is pressed and mouse moves, pan horizontally camera # and rotate vertically if Input.is_action_just_pressed ( "ui_strafe" ): Input.set_mouse_mode(Input.MOUSE_MODE_HIDDEN) elif Input.is_action_just_released ( "ui_strafe" ): Input.set_mouse_mode(Input.MOUSE_MODE_VISIBLE) if Input.is_mouse_button_pressed( 2 ): if event is InputEventMouseMotion: camera_rotate_y -= event.relative.x *0.01 if camera_rotate_y >= PI: camera_rotate_y -= TWO_PI elif camera_rotate_y <= -PI: camera_rotate_y += TWO_PI $camera_root/horizontal_root.rotate_y( -event.relative.x *0.01 ) var new_camera_rotate_x = camera_rotate_x + event.relative.y * 0.01 if new_camera_rotate_x <= PI_2 and new_camera_rotate_x >= - PI_2: $camera_root/horizontal_root/vertical_root.rotate_x( event.relative.y * 0.01 ) camera_rotate_x = new_camera_rotate_x func _physics_process(delta): # Add the gravity. if not is_on_floor(): motion_velocity.y -= gravity * delta # Handle Jump. if Input.is_action_just_pressed("ui_accept") and is_on_floor(): motion_velocity.y = JUMP_FORCE if not Input.is_action_pressed( "ui_strafe" ): var y = 0 if Input.is_action_pressed("ui_right"): y -= 1 if Input.is_action_pressed("ui_left"): y += 1 if y != 0: var dt = y * delta * speed_rotate_1sec camera_rotate_y += dt if camera_rotate_y >= PI: camera_rotate_y -= TWO_PI elif camera_rotate_y <= -PI: camera_rotate_y += TWO_PI $camera_root/horizontal_root.rotate_y( dt ) # Get the input direction and handle the movement/deceleration. # As good practice, you should replace UI actions with custom gameplay actions. var input_dir = Input.get_vector("ui_strafe_right", "ui_strafe_left", "ui_down", "ui_up") if Input.is_action_pressed("ui_strafe"): input_dir = Input.get_vector("ui_right", "ui_left", "ui_down", "ui_up") var direction = (transform.basis * Vector3(input_dir.x, 0, input_dir.y)).normalized() if direction: motion_velocity.x = direction.x * SPEED motion_velocity.z = direction.z * SPEED else: motion_velocity.x = move_toward(motion_velocity.x, 0, SPEED) motion_velocity.z = move_toward(motion_velocity.z, 0, SPEED) move_and_slide() func _process( delta ): var diff = camera_rotate_y - player_rotate_y if diff > PI: diff = camera_rotate_y - player_rotate_y - TWO_PI elif diff < -PI: diff = camera_rotate_y - player_rotate_y + TWO_PI var absdiff = diff if absdiff < 0.0: absdiff = -absdiff if absdiff <= 0.5 * delta: rotate_y( diff ) $camera_root/horizontal_root.rotate_y( -diff ) # $Mesh/character.rotate_y( diff ) player_rotate_y = camera_rotate_y else: if diff >= 0.0: diff = delta * speed_rotate_1sec else: diff = -delta * speed_rotate_1sec rotate_y( diff ) $camera_root/horizontal_root.rotate_y( -diff ) #$Mesh/character.rotate_y( diff ) player_rotate_y += diff