Guest User

player.gd

a guest
Jul 26th, 2024
30
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
GDScript 2.21 KB | Source Code | 0 0
  1. extends CharacterBody3D
  2.  
  3. @onready var camera_3d = $Eyes
  4. @onready var hold_point = $Eyes/HoldPoint
  5. @onready var hold_check = $Eyes/HoldCheck
  6.  
  7.  
  8. @export var sens = 0.25
  9.  
  10. const SPEED = 5.0
  11. const JUMP_VELOCITY = 4.5
  12.  
  13. var held_obj : RigidBody3D;
  14. var held_parent;
  15.  
  16. # Get the gravity from the project settings to be synced with RigidBody nodes.
  17. var gravity = ProjectSettings.get_setting("physics/3d/default_gravity")
  18.  
  19.  
  20. func _ready():
  21.     Input.mouse_mode = Input.MOUSE_MODE_CAPTURED
  22.  
  23.  
  24. func _input(event):
  25.     if event is InputEventMouseMotion:
  26.         rotate_y(-deg_to_rad(event.relative.x) * sens)
  27.         camera_3d.rotate_x(-deg_to_rad(event.relative.y) * sens)
  28.         camera_3d.rotation.x = clamp(camera_3d.rotation.x, deg_to_rad(-90), deg_to_rad(90))
  29.  
  30. func to_positive_radians(vec: Vector3) -> Vector3:
  31.     return Vector3(
  32.         vec.x,
  33.         fmod(vec.y, TAU) + (TAU if vec.y < 0.0 else 0.0),
  34.         vec.z
  35.     )
  36.    
  37.  
  38. func _physics_process(delta):
  39.     # Add the gravity.
  40.     if not is_on_floor():
  41.         velocity.y -= gravity * delta
  42.  
  43.     # Handle jump.
  44.     if Input.is_action_just_pressed("action_jump") and is_on_floor():
  45.         velocity.y = JUMP_VELOCITY
  46.  
  47.     # Get the input direction and handle the movement/deceleration.
  48.     # As good practice, you should replace UI actions with custom gameplay actions.
  49.     var input_dir = Input.get_vector("move_left", "move_right", "move_forward", "move_back")
  50.     var direction = (transform.basis * Vector3(input_dir.x, 0, input_dir.y)).normalized()
  51.     if direction:
  52.         velocity.x = direction.x * SPEED
  53.         velocity.z = direction.z * SPEED
  54.     else:
  55.         velocity.x = move_toward(velocity.x, 0, SPEED)
  56.         velocity.z = move_toward(velocity.z, 0, SPEED)
  57.        
  58.     if Input.is_action_just_pressed("action_pickup"):
  59.         if hold_check.get_collider() and hold_check.get_collider() is RigidBody3D and hold_check.get_collider().mass <= 50:
  60.             held_obj = hold_check.get_collider()
  61.             held_parent = held_obj.get_parent()
  62.            
  63.     if held_obj:
  64.         held_obj.linear_velocity = (hold_point.global_position - held_obj.global_position) * 10
  65.         held_obj.angular_velocity = (hold_point.global_rotation - held_obj.global_rotation) * 10
  66.        
  67.     if Input.is_action_just_released("action_pickup"):
  68.         if held_obj:
  69.             held_obj.sleeping = false
  70.             held_obj = null
  71.  
  72.     move_and_slide()
  73.  
Advertisement
Add Comment
Please, Sign In to add comment