Guest User

player.gd (fixed)

a guest
Jul 26th, 2024
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
GDScript 2.46 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.  
  15. var vel
  16.  
  17. # Get the gravity from the project settings to be synced with RigidBody nodes.
  18. var gravity = ProjectSettings.get_setting("physics/3d/default_gravity")
  19.  
  20.  
  21. func _ready():
  22.     Input.mouse_mode = Input.MOUSE_MODE_CAPTURED
  23.  
  24.  
  25. func _input(event):
  26.     if event is InputEventMouseMotion:
  27.         rotate_y(-deg_to_rad(event.relative.x) * sens)
  28.         camera_3d.rotate_x(-deg_to_rad(event.relative.y) * sens)
  29.         camera_3d.rotation.x = clamp(camera_3d.rotation.x, deg_to_rad(-90), deg_to_rad(90))
  30.  
  31. func _physics_process(delta):
  32.     # Add the gravity.
  33.     if not is_on_floor():
  34.         velocity.y -= gravity * delta
  35.  
  36.     # Handle jump.
  37.     if Input.is_action_just_pressed("action_jump") and is_on_floor():
  38.         velocity.y = JUMP_VELOCITY
  39.  
  40.     # Get the input direction and handle the movement/deceleration.
  41.     # As good practice, you should replace UI actions with custom gameplay actions.
  42.     var input_dir = Input.get_vector("move_left", "move_right", "move_forward", "move_back")
  43.     var direction = (transform.basis * Vector3(input_dir.x, 0, input_dir.y)).normalized()
  44.     if direction:
  45.         velocity.x = direction.x * SPEED
  46.         velocity.z = direction.z * SPEED
  47.     else:
  48.         velocity.x = move_toward(velocity.x, 0, SPEED)
  49.         velocity.z = move_toward(velocity.z, 0, SPEED)
  50.        
  51.     if Input.is_action_just_pressed("action_pickup"):
  52.         if hold_check.get_collider() and hold_check.get_collider() is RigidBody3D and hold_check.get_collider().mass <= 50:
  53.             held_obj = hold_check.get_collider()
  54.            
  55.     if held_obj:
  56.         held_obj.linear_velocity = (hold_point.global_position - held_obj.global_position) * 10
  57.        
  58.         var hp: Vector3 = hold_point.global_rotation
  59.         var ho: Vector3 = held_obj.global_rotation
  60.        
  61.         var hpq: Quaternion = Quaternion.from_euler(hp)
  62.         var hoq: Quaternion = Quaternion.from_euler(ho)
  63.        
  64.         var hpqi: Quaternion = hpq.inverse()
  65.         var hoqi: Quaternion = hoq.inverse()
  66.        
  67.         vel = (hpq * hoqi)
  68.         var velv = Vector3(vel.x, vel.y, vel.z)
  69.        
  70.         var threshold = 1
  71.         if velv.x > threshold:
  72.             velv.x = 0
  73.         if velv.y > threshold:
  74.             velv.y = 0
  75.         if velv.z > threshold:
  76.             velv.z = 0
  77.        
  78.         held_obj.angular_velocity = velv * 180
  79.        
  80.     if Input.is_action_just_released("action_pickup"):
  81.         if held_obj:
  82.             held_obj.sleeping = false
  83.             held_obj = null
  84.  
  85.     move_and_slide()
  86.  
Advertisement
Add Comment
Please, Sign In to add comment