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 timer_rotate:Timer var speed_rotate_1sec = PI var max_angle = PI / 2 const TWO_PI = 2.0 * PI const PI_2 = PI / 2.0 func _init(): # timer_rotate = Timer.new() # add_child(timer_rotate) # timer_rotate.autostart = false # timer_rotate.wait_time = 1.0 # timer_rotate.connect("timeout", update_rotate_start.bind()) 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_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 ) # if (roty >= 0.0 and max_angle > roty) or (roty <= 0.0 and max_angle > -roty): # $camera_root/horizontal_root.rotate_y( event.relative.x *0.01 ) # camera_rotate = roty # else: # $Mesh/character.rotate_y( event.relative.x *0.01 ) # $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 # timer_rotate.autostart = false # timer_rotate.wait_time = 1.0 # if timer_rotate.is_connected("timeout", update_rotate.bind()): # timer_rotate.stop() # timer_rotate.disconnect("timeout", update_rotate.bind()) # timer_rotate.connect("timeout", update_rotate_start.bind()) # timer_rotate.start(1.0) # print("Update timer") # else: # #timer_rotate.stop() # timer_rotate.start(1.0) # print("launch timer") 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 # # 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_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: $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 $Mesh/character.rotate_y( diff ) player_rotate_y += diff #func update_rotate_start(): # print("update_rotate_start") # var camera = $camera_root/horizontal_root.get_basis() # var player = $Mesh/character.get_basis() # timer_rotate.disconnect("timeout", update_rotate_start.bind()) # timer_rotate.connect("timeout", update_rotate.bind()) # timer_rotate.start(0.005) # # #func update_rotate(): # print("update_rotate") # var step = 0.0 # if camera_rotate >= 0.1: # step = -0.05 # elif camera_rotate <= -0.1: # step = +0.05 # $Mesh/character.rotate_y( -step ) # #$camera_root/horizontal_root.rotate_y( step / 2.0 ) # camera_rotate += step ## if (roty >= 0.0 and max_angle > roty) or (roty <= 0.0 and max_angle > -roty): ## $camera_root/horizontal_root.rotate_y( event.relative.x *0.01 ) ## current_rotate = roty ## else: ## $Mesh/character.rotate_y( event.relative.x *0.01 ) ## $camera_root/horizontal_root.rotate_y( event.relative.x *0.01 ) # if camera_rotate <= 0.1 and camera_rotate >= -0.1: # timer_rotate.stop()