Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- extends CharacterBody3D
- var speed
- const WALK_SPEED = 5.0
- const SPRINT_SPEED = 8.0
- const JUMP_VELOCITY = 4.8
- const SENSITIVITY = 0.004
- #bob variables
- const BOB_FREQ = 2.4
- const BOB_AMP = 0.08
- var t_bob = 0.0
- #fov variables
- const BASE_FOV = 70
- const FOV_CHANGE = 1.5
- var gravity = 9.8
- @onready var camera = $CharRoot/Model/Armature/Skeleton3D/BoneAttachment3D/CameraRotate/Camera
- @onready var cam_rotate = $CharRoot/Model/Armature/Skeleton3D/BoneAttachment3D/CameraRotate
- @onready var anim_tree = $AnimationTree
- func _ready():
- Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)
- func _unhandled_input(event):
- if event is InputEventMouseMotion:
- rotate_y(-event.relative.x * SENSITIVITY)
- cam_rotate.rotate_x(-event.relative.y * SENSITIVITY)
- cam_rotate.rotation.x = clamp(cam_rotate.rotation.x, deg_to_rad(-40), deg_to_rad(60))
- """
- cam_rotate.rotate_x(deg_to_rad(event.relative.y * SENSITIVITY))
- rotate_y(deg_to_rad(event.relative.x * SENSITIVITY * -1))
- var camera_rot = cam_rotate.rotation_degrees
- camera_rot.x = clamp(camera_rot.x, -70, 70)
- cam_rotate.rotation_degrees = camera_rot
- """
- func _physics_process(delta):
- # Add the gravity.
- if not is_on_floor():
- velocity.y -= gravity * delta
- # Handle Jump.
- if Input.is_action_just_pressed("jump") and is_on_floor():
- velocity.y = JUMP_VELOCITY
- # Handle Sprint.
- if Input.is_action_pressed("sprint"):
- speed = SPRINT_SPEED
- else:
- speed = WALK_SPEED
- # Get the input direction and handle the movement/deceleration.
- var input_dir = Input.get_vector("move_left", "move_right", "move_forward", "move_back")
- var direction = (camera.transform.basis * transform.basis * Vector3(input_dir.x, 0, input_dir.y)).normalized()
- if is_on_floor():
- if direction:
- velocity.x = direction.x * speed
- velocity.z = direction.z * speed
- else:
- velocity.x = lerp(velocity.x, direction.x * speed, delta * 7.0)
- velocity.z = lerp(velocity.z, direction.z * speed, delta * 7.0)
- else:
- velocity.x = lerp(velocity.x, direction.x * speed, delta * 3.0)
- velocity.z = lerp(velocity.z, direction.z * speed, delta * 3.0)
- # Head bob
- "t_bob += delta * velocity.length() * float(is_on_floor())"
- "camera.transform.origin = _headbob(t_bob)"
- # FOV
- var velocity_clamped = clamp(velocity.length(), 0.5, SPRINT_SPEED * 2)
- var target_fov = BASE_FOV + FOV_CHANGE * velocity_clamped
- camera.fov = lerp(camera.fov, target_fov, delta * 8.0)
- anim_tree.set("parameters/conditions/idle", input_dir==Vector2.ZERO && is_on_floor())
- anim_tree.set("parameters/conditions/walk", (input_dir.y == 1 || input_dir.y == -1) && is_on_floor())
- anim_tree.set("parameters/conditions/sraifLeft", input_dir.x == -1 && is_on_floor())
- anim_tree.set("parameters/conditions/sraifRight", input_dir.x == 1 && is_on_floor())
- anim_tree.set("parameters/conditions/falling", !is_on_floor())
- anim_tree.set("parameters/conditions/landed", is_on_floor())
- move_and_slide()
- func _headbob(time) -> Vector3:
- var pos = Vector3.ZERO
- pos.y = sin(time * BOB_FREQ) * BOB_AMP
- pos.x = cos(time * BOB_FREQ / 2) * BOB_AMP
- return pos
Advertisement
Add Comment
Please, Sign In to add comment