Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- how to add animations to this script: extends KinematicBody
- export var speed = 3
- var path = []
- var cur_path_idx = 0
- var target = null
- var velocity = Vector3.ZERO
- var threshold = .1
- onready var nav = get_parent()
- func _ready():
- yield(owner, "ready")
- target = owner.player
- func _physics_process(delta):
- if path.size() > 0:
- move_to_target()
- func move_to_target():
- if cur_path_idx >= path.size():
- return
- if global_transform.origin.distance_to(path[cur_path_idx]) < threshold:
- cur_path_idx += 1
- else:
- var direction = path[cur_path_idx] - global_transform.origin
- velocity = direction.normalized() * speed
- move_and_slide(velocity, Vector3.UP)
- func get_target_path(target_pos):
- path = nav.get_simple_path(global_transform.origin, target_pos)
- cur_path_idx = 0
- func _on_Timer_timeout():
- get_target_path(target.global_transform.origin)
- To add animations to this script, you will need to do the following:
- Add an animation player node to your scene as a child of the node that extends KinematicBody.
- Create your desired animation(s) in the Animation Editor and assign them to the animation player node.
- 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.
- 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.
- Here is an example of how you could implement this:
- Copy code
- # Add this at the top of the script
- onready var anim_player = $AnimationPlayer
- func _physics_process(delta):
- if path.size() > 0:
- move_to_target()
- # Check if the distance to the target is greater than the threshold
- if global_transform.origin.distance_to(path[cur_path_idx]) > threshold:
- # Play the "walk" animation
- anim_player.play("walk")
- # Check if the distance to the target is less than or equal to the threshold
- elif global_transform.origin.distance_to(path[cur_path_idx]) <= threshold:
- # Stop the "walk" animation
- anim_player.stop()
- 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.
- 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:
- Copy code
- # Add this at the top of the script
- onready var anim_player = $AnimationPlayer
- func _physics_process(delta):
- if path.size() > 0:
- move_to_target()
- # Check if the distance to the target is greater than the threshold
- if global_transform.origin.distance_to(path[cur_path_idx]) > threshold:
- # If the "walk" animation is not already playing, start it
- if not anim_player.is_playing("walk"):
- anim_player.play("walk")
- # Check if the distance to the target is less than or equal to the threshold
- elif global_transform.origin.distance_to(path[cur_path_idx]) <= threshold:
- # If the "walk" animation is playing, stop it
- if anim_player.is_playing("walk"):
- anim_player.stop()
- 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.
- Regenerate response
- 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