Advertisement
Gavr1loff

GD Player code

May 31st, 2024
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
GDScript 2.27 KB | Gaming | 0 0
  1. ```
  2. extends CharacterBody3D
  3.  
  4.  
  5. @onready var head = $head
  6. @onready var cam = $head/Camera3D
  7.  
  8.  
  9. var accel = 6
  10. var SPEED = 5.0
  11. var JUMP_VELOCITY = 4.5
  12. var crouched = false #определяет приседает игрок или нет
  13. var input_dir = Vector3(0,0,0) #направление нажатия кнопок
  14. var direction = Vector3() # направление игрок
  15. var sens = 0.005
  16. var gravity = ProjectSettings.get_setting("physics/3d/default_gravity")
  17.  
  18. func _ready():
  19.     Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)
  20.  
  21. func _input(event: InputEvent): #повороты мышкой
  22.     if Input.is_action_just_pressed("ui_cancel"):
  23.         Input.set_mouse_mode(Input.MOUSE_MODE_VISIBLE)
  24.     if Input.get_mouse_mode() == Input.MOUSE_MODE_CAPTURED:
  25.         if event is InputEventMouseMotion:
  26.             head.rotate_y(-event.relative.x * sens)
  27.             cam.rotate_x(-event.relative.y * sens)
  28.             cam.rotation.x = clamp(cam.rotation.x,deg_to_rad(-89),deg_to_rad(89))
  29.            
  30. func _physics_process(delta):
  31.     if Input.is_action_pressed("+crouch"): #приседание
  32.         crouched = true
  33.         SPEED = 2.5
  34.         $CollisionShape3D.scale.y = lerp($CollisionShape3D.scale.y,0.4,0.4)
  35.         $CollisionShape3D.position.y = lerp($CollisionShape3D.position.y, 0.66,0.4)
  36.         head.position.y = lerp(head.position.y, 1.0, 0.3)
  37.     else:
  38.         crouched = false
  39.         SPEED = 5
  40.         $CollisionShape3D.scale.y = lerp($CollisionShape3D.scale.y, 1.0 ,0.4)
  41.         $CollisionShape3D.position.y = lerp($CollisionShape3D.position.y, 1.143,0.4)
  42.         head.position.y = lerp(head.position.y, 1.85 , 0.3)
  43.  
  44.     if not is_on_floor(): #гравитация
  45.         velocity.y -= gravity * delta
  46.  
  47.     if Input.is_action_just_pressed("ui_accept") and is_on_floor() and crouched == false: # прыжок
  48.         velocity.y = JUMP_VELOCITY
  49.        
  50.     if is_on_floor() and Input.is_action_pressed("+shift"):
  51.         SPEED = 10
  52.     else:
  53.         SPEED = 5
  54.     ################################################хождение туда сюда
  55.     input_dir = Input.get_vector("+s", "+w", "+a", "+d")
  56.     direction = ($head.transform.basis * Vector3(input_dir.x, 0, input_dir.y)).normalized()
  57.  
  58.     velocity.x = lerp(velocity.x ,direction.x * SPEED, accel * delta)
  59.     velocity.z = lerp(velocity.z ,direction.z * SPEED, accel * delta)
  60.     move_and_slide()
  61.     ###################################################--
  62.  
  63. ```
  64.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement