Advertisement
julong

Untitled

Apr 13th, 2022
1,027
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.51 KB | None | 0 0
  1. extends KinematicBody
  2.  
  3. # ------------------------------------------------------------
  4. # Use 'W', 'S' keys for move + 'Shift'/'Ctrl' to sprint/creep.
  5. # For enter/exit view mode press 'Tab'.
  6. # Script author: Warrpy
  7. # -------------------------------------------------------------
  8.  
  9. var velocity = Vector3()
  10. var direction = Vector3()
  11. var speed = 0
  12. var gravity = -14
  13. var accelerating = false
  14. var define_direction = false
  15. var initial_speed = 0
  16. var forward = false
  17. var backward = false
  18. var view_mode = false
  19. var slowly = 0
  20.  
  21. # variables that can be edited in the Inspector tab.
  22. export var acceleration = 0.01
  23. export var deceleration = 0.05
  24. export var max_speed = 5
  25. export var angle_ease = 5
  26.  
  27. var jump = false
  28. var fall = false
  29. var creep = false
  30. var sprint = false
  31.  
  32. var boost = 0
  33.  
  34. onready var axis = get_parent().get_node("Axis")
  35. onready var animator = $"atomic-robot/AnimationPlayer"
  36.  
  37. # ------------------------------------------------------------------
  38. # Robot 3D model from Atomic Game Engine examples, author Bartheinz.
  39. # ------------------------------------------------------------------
  40.  
  41. func _physics_process(delta):
  42.     direction = Vector3()
  43.     if Input.is_action_pressed("forward"):
  44.         forward = true
  45.         direction.z = 1
  46.         define_direction = true
  47.         # walking animation control block.
  48.         if not jump and not fall and not sprint and not creep:
  49.             animator.play("Walking", 0.2)
  50.     else:
  51.         forward = false
  52.         # default pose.
  53.         if not jump and not fall and not backward:
  54.             animator.play("Default", 0.3)
  55.             # previous animation/s will be blend to "Default" for 0.3 sec.
  56.     if Input.is_action_pressed("backward"):
  57.         backward = true
  58.         direction.z = -1
  59.         define_direction = false
  60.         # walking backwards.
  61.         if not jump and not fall and not sprint:
  62.             animator.play_backwards("Walking")
  63.     else:
  64.         backward = false
  65.     if Input.is_action_pressed("sprint"):
  66.         if forward and not jump and not fall:
  67.             sprint = true
  68.             boost = 1.4
  69.             animator.play("Running", 0.3)
  70.     else:
  71.         sprint = false
  72.         boost = 1
  73.     if Input.is_action_pressed("creep"):
  74.         if forward:
  75.             creep = true
  76.             animator.play("Creep", 0.3)
  77.     else:
  78.         creep = false
  79.     if Input.is_action_just_pressed("view_mode"):
  80.         if view_mode:
  81.             view_mode = false
  82.         else:
  83.             view_mode = true
  84.        
  85.     if forward or backward:
  86.         if not view_mode:
  87.             # smoothly rotates from one rotation to another.
  88.             slowly = lerp_angle(rotation.y, axis.rotation.y, deg2rad(angle_ease))
  89.             rotation.y = slowly
  90.         # acceleration.
  91.         initial_speed = lerp(speed, max_speed, acceleration)
  92.         speed = initial_speed
  93.     else:
  94.         # determines where the movement was directed last,
  95.         # forward or backward.
  96.         if define_direction:
  97.             direction.z = 1
  98.         else:
  99.             direction.z = -1
  100.         # deceleration.
  101.         initial_speed = lerp(speed, 0, deceleration)
  102.         speed = initial_speed
  103.    
  104.     direction = direction.normalized()
  105.     direction *= speed * boost
  106.    
  107.     # rotates the direction of movement depending on the rotation.
  108.     # easier, body moves where it is directed/faced.
  109.     direction = direction.rotated(Vector3(0, 1, 0), rotation.y)
  110.    
  111.     velocity.x = direction.x
  112.     velocity.z = direction.z
  113.     velocity.y += gravity * delta
  114.    
  115.     velocity = move_and_slide(velocity, Vector3.UP)
  116.    
  117.     # if all boolean values are false, the body is falling.
  118.     if not is_on_floor() and not jump and not fall:
  119.         fall = true
  120.         animator.play("Fall")
  121.    
  122.     if is_on_floor() and fall:
  123.         fall = false
  124.    
  125.     if is_on_floor() and jump:
  126.         jump = false
  127.    
  128.     if is_on_floor() and Input.is_action_pressed("jump"):
  129.         velocity.y = 10
  130.         animator.play("Jump")
  131.         jump = true
  132.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement