Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- extends CharacterBody3D
- @onready var camera = $Cam/Camera
- @onready var cam = $Cam
- const MOUSE_SENSITIVITY = 0.25
- const GRAVITY = -20.0
- const JUMP_SPEED = 4.5
- const SPEED = 6
- const ACCEL = 12.0
- const AIR_ACCEL = 1.5
- var vel = Vector3.ZERO
- var currentvel = Vector3.ZERO
- var dir = Vector3.ZERO
- func _ready():
- Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)
- func _physics_process(delta):
- dir = Vector3.ZERO
- if Input.is_action_pressed("Forward"):
- dir -= camera.global_transform.basis.z
- if Input.is_action_pressed("Back"):
- dir += camera.global_transform.basis.z
- if Input.is_action_pressed("Left"):
- dir -= camera.global_transform.basis.x
- if Input.is_action_pressed("Right"):
- dir += camera.global_transform.basis.x
- dir = dir.normalized()
- var target_vel = dir * SPEED
- var accel = ACCEL if is_on_floor() else AIR_ACCEL
- currentvel = currentvel.lerp(target_vel, accel * delta)
- vel.x = currentvel.x
- vel.z = currentvel.z
- set_velocity(vel)
- set_up_direction(Vector3.UP)
- set_floor_stop_on_slope_enabled(true)
- set_max_slides(4)
- set_floor_max_angle(deg_to_rad(45))
- move_and_slide()
- vel = velocity
- vel.y += GRAVITY * delta
- if Input.is_action_just_pressed("Jump") and is_on_floor():
- vel.y = JUMP_SPEED
- func _input(event):
- if event is InputEventMouseMotion:
- cam.rotate_x(deg_to_rad(event.relative.y * MOUSE_SENSITIVITY * 1))
- cam.rotation_degrees.x = clamp(cam.rotation_degrees.x, -40, 35)
- self.rotate_y(deg_to_rad(event.relative.x * MOUSE_SENSITIVITY * -1))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement