Advertisement
desync1337

godot 4 controller

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