Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- extends KinematicBody
- export (float) var walking_speed = 100
- export (float) var jump_force = 30
- export (Vector3) var gravity = Vector3(0, -50, 0)
- export(float, 0.1, 1.0) var sensitivity_x = 0.6
- export(float, 0.1, 1.0) var sensitivity_y = 0.6
- export (float) var acceleration = 80.0
- var velocity := Vector3.ZERO
- var is_grounded = false
- var mouse_motion = Vector2()
- onready var camera = $Camera
- func _ready() -> void:
- Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)
- func _input(event : InputEvent) -> void:
- if event is InputEventMouseMotion:
- mouse_motion = event.relative
- elif event.is_action_pressed("ui_cancel"):
- get_tree().quit()
- func handle_camera_rotation(delta : float) -> void:
- rotate_y( (PI*0.1) * - mouse_motion.x * sensitivity_x * delta)
- camera.rotate_x( (PI*0.1) * -mouse_motion.y * sensitivity_y * delta )
- camera.rotation.x = clamp(camera.rotation.x, -PI*0.5, PI*0.5)
- mouse_motion = Vector2()
- func _physics_process(delta : float) -> void:
- handle_camera_rotation(delta)
- var dir = Vector3.ZERO
- if Input.is_action_pressed("move_forward"):
- dir -= transform.basis.z
- if Input.is_action_pressed("move_back"):
- dir += transform.basis.z
- if Input.is_action_pressed("move_left"):
- dir -= transform.basis.x
- if Input.is_action_pressed("move_right"):
- dir += transform.basis.x
- dir = dir.normalized() * walking_speed
- velocity = velocity.move_toward(Vector3(dir.x, velocity.y, dir.z), acceleration*delta)
- velocity += gravity * delta
- is_grounded = is_on_floor()
- if is_grounded and Input.is_action_just_pressed("jump"):
- velocity.y = jump_force
- is_grounded = false
- var snap:Vector3
- if !is_grounded:
- snap = Vector3.ZERO
- else:
- snap = Vector3.DOWN * 0.2
- velocity = move_and_slide_with_snap(velocity, snap, Vector3.UP, true)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement