Advertisement
metalx1000

Godot Doom Sprite 3D Script

Sep 5th, 2021
3,917
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. extends KinematicBody
  2.  
  3. export var anim_col = 0
  4. export var speed = 7
  5. export var gravity = 20
  6. const ACCEL_DEFAULT = 7
  7. const ACCEL_AIR = 1
  8. onready var accel = ACCEL_DEFAULT
  9.  
  10. var direction = Vector3()
  11. var velocity = Vector3()
  12. var gravity_vec = Vector3()
  13. var movement = Vector3()
  14. var snap
  15. var jump = 10
  16.  
  17. onready var sprite = $Sprite3D
  18. onready var ani_player = $AnimationPlayer
  19.  
  20. var camera = null
  21.  
  22. func _ready():
  23.     add_to_group("Imps")
  24.     ani_player.play("anim_col")
  25.  
  26. func _physics_process(delta):
  27.     follow_camera()
  28.     jump(delta)
  29.     move(delta)
  30.  
  31. func set_camera(c):
  32.     camera = c
  33.    
  34. func follow_camera():
  35.     if camera == null:
  36.         return
  37.    
  38.     var p_fwd = -camera.global_transform.basis.z
  39.     var fwd = global_transform.basis.z
  40.     var left = global_transform.basis.x
  41.    
  42.     var l_dot = left.dot(p_fwd)
  43.     var f_dot = fwd.dot(p_fwd)
  44.     var row = 0
  45.     sprite.flip_h = false
  46.     if f_dot < -8.5:
  47.         row = 0 # front sprite
  48.     elif f_dot > 8.5:
  49.         row = 4 # back sprite
  50.     else:
  51.         sprite.flip_h = l_dot > 0
  52.         if abs(f_dot) < 0.3:
  53.             row = 2 # left sprite
  54.         elif f_dot < 0:
  55.             row = 1 # forward left sprite
  56.         else:
  57.             row = 3 # back left sprite
  58.     sprite.frame = anim_col + row * 4
  59.  
  60. func jump(delta):
  61.     #jumping and gravity
  62.     if is_on_floor():
  63.         snap = -get_floor_normal()
  64.         accel = ACCEL_DEFAULT
  65.         gravity_vec = Vector3.ZERO
  66.     else:
  67.         snap = Vector3.DOWN
  68.         accel = ACCEL_AIR
  69.         gravity_vec += Vector3.DOWN * gravity * delta
  70.        
  71.     #if Input.is_action_just_pressed("jump") and is_on_floor():
  72.     #   snap = Vector3.ZERO
  73.     #   gravity_vec = Vector3.UP * jump
  74.  
  75. func move(delta):
  76.     #make it move
  77.     velocity = velocity.linear_interpolate(direction * speed, accel * delta)
  78.     movement = velocity + gravity_vec
  79.    
  80.     move_and_slide_with_snap(movement, snap, Vector3.UP)
  81.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement