Advertisement
chris33556

AI alternative chatGPT script

Dec 20th, 2022
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.69 KB | None | 0 0
  1. how to add animations to this script: extends KinematicBody
  2.  
  3. export var speed = 3
  4.  
  5. var path = []
  6. var cur_path_idx = 0
  7. var target = null
  8. var velocity = Vector3.ZERO
  9. var threshold = .1
  10.  
  11. onready var nav = get_parent()
  12.  
  13. func _ready():
  14. yield(owner, "ready")
  15. target = owner.player
  16.  
  17. func _physics_process(delta):
  18. if path.size() > 0:
  19. move_to_target()
  20.  
  21. func move_to_target():
  22. if cur_path_idx >= path.size():
  23. return
  24.  
  25. if global_transform.origin.distance_to(path[cur_path_idx]) < threshold:
  26. cur_path_idx += 1
  27.  
  28. else:
  29. var direction = path[cur_path_idx] - global_transform.origin
  30. velocity = direction.normalized() * speed
  31. move_and_slide(velocity, Vector3.UP)
  32.  
  33. func get_target_path(target_pos):
  34. path = nav.get_simple_path(global_transform.origin, target_pos)
  35. cur_path_idx = 0
  36.  
  37. func _on_Timer_timeout():
  38. get_target_path(target.global_transform.origin)
  39. To add animations to this script, you will need to do the following:
  40.  
  41. Add an animation player node to your scene as a child of the node that extends KinematicBody.
  42. Create your desired animation(s) in the Animation Editor and assign them to the animation player node.
  43. In the _physics_process() function, add a conditional statement to check if the distance between the current position and the target position is greater than a certain threshold. If it is, play the desired animation.
  44. To stop the animation, add another conditional statement to check if the distance between the current position and the target position is less than or equal to the threshold. If it is, stop the animation.
  45. Here is an example of how you could implement this:
  46.  
  47. Copy code
  48. # Add this at the top of the script
  49. onready var anim_player = $AnimationPlayer
  50.  
  51. func _physics_process(delta):
  52. if path.size() > 0:
  53. move_to_target()
  54.  
  55. # Check if the distance to the target is greater than the threshold
  56. if global_transform.origin.distance_to(path[cur_path_idx]) > threshold:
  57. # Play the "walk" animation
  58. anim_player.play("walk")
  59.  
  60. # Check if the distance to the target is less than or equal to the threshold
  61. elif global_transform.origin.distance_to(path[cur_path_idx]) <= threshold:
  62. # Stop the "walk" animation
  63. anim_player.stop()
  64. This code will play the "walk" animation when the distance to the target is greater than the threshold, and stop the animation when the distance is less than or equal to the threshold. You can adjust the threshold value to control when the animation starts and stops.
  65.  
  66. You can also use the anim_player.is_playing() function to check if an animation is currently playing, and use that to control the animation. For example:
  67.  
  68. Copy code
  69. # Add this at the top of the script
  70. onready var anim_player = $AnimationPlayer
  71.  
  72. func _physics_process(delta):
  73. if path.size() > 0:
  74. move_to_target()
  75.  
  76. # Check if the distance to the target is greater than the threshold
  77. if global_transform.origin.distance_to(path[cur_path_idx]) > threshold:
  78. # If the "walk" animation is not already playing, start it
  79. if not anim_player.is_playing("walk"):
  80. anim_player.play("walk")
  81.  
  82. # Check if the distance to the target is less than or equal to the threshold
  83. elif global_transform.origin.distance_to(path[cur_path_idx]) <= threshold:
  84. # If the "walk" animation is playing, stop it
  85. if anim_player.is_playing("walk"):
  86. anim_player.stop()
  87. This will ensure that the "walk" animation is only played when the distance to the target is greater than the threshold, and only stopped when the distance is less than or equal to the threshold.
  88.  
  89.  
  90.  
  91. Regenerate response
  92.  
  93. ChatGPT Dec 15 Version. Free Research Preview. Our goal is to make AI systems more natural and safe to interact with. Your feedback will help us improve.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement