Guest User

Player.gd

a guest
Jun 24th, 2024
242
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class_name  player extends CharacterBody3D
  2.  
  3.  
  4. const SPEED = 5.0
  5. const JUMP_VELOCITY = 4.5
  6. static var mouse_locked: bool = true :
  7.     set(val):
  8.         if (val == true):
  9.             Input.mouse_mode = Input.MOUSE_MODE_CAPTURED
  10.         else:
  11.             Input.mouse_mode = Input.MOUSE_MODE_VISIBLE
  12.        
  13.         mouse_locked = val
  14.     get:
  15.         return mouse_locked
  16.  
  17. @export var cam: Camera3D
  18. @export var invertY: bool
  19. @export var isGreenRuneInabled: bool
  20.  
  21. func _ready() -> void:
  22.     Input.mouse_mode = Input.MOUSE_MODE_CAPTURED
  23.  
  24. func _physics_process(delta: float) -> void:
  25.     # Add the gravity.
  26.     if not is_on_floor():
  27.         velocity += get_gravity() * delta
  28.  
  29.     # Handle jump.
  30.     if Input.is_action_just_pressed("ui_accept") and is_on_floor():
  31.         velocity.y = JUMP_VELOCITY
  32.  
  33.     # Get the input direction and handle the movement/deceleration.
  34.     # As good practice, you should replace UI actions with custom gameplay actions.
  35.     var input_dir := Input.get_vector("ui_left", "ui_right", "ui_up", "ui_down")
  36.     var direction := (transform.basis * Vector3(input_dir.x, 0, input_dir.y)).normalized()
  37.     if direction:
  38.         velocity.x = direction.x * SPEED
  39.         velocity.z = direction.z * SPEED
  40.     else:
  41.         velocity.x = move_toward(velocity.x, 0, SPEED)
  42.         velocity.z = move_toward(velocity.z, 0, SPEED)
  43.  
  44.     move_and_slide()
  45.    
  46. func _input(event: InputEvent) -> void:
  47.     if event is InputEventMouseMotion:
  48.         if mouse_locked:
  49.             cam.rotate_x(-event.relative.y*0.003)
  50.             cam.rotation_degrees.x = clampf(cam.rotation_degrees.x, -90.0, 90.0)
  51.             var inverted = 1
  52.             if invertY:
  53.                 inverted = -1
  54.             rotate_y((inverted*event.relative.x)*0.003)
  55.  
Advertisement
Add Comment
Please, Sign In to add comment