Advertisement
ChrisTutorials

ResourceGatheringRPG Player Controller Script

Mar 28th, 2024
612
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. extends CharacterBody2D
  2.  
  3. class_name Player
  4.  
  5. @export var speed : float = 100.0
  6.  
  7. @onready var animation_tree : AnimationTree = $AnimationTree
  8.  
  9. var direction : Vector2 = Vector2.ZERO
  10.  
  11. func _ready():
  12.     animation_tree.active = true
  13.    
  14. func _process(delta):
  15.     update_animation_parameters()
  16.  
  17. func _physics_process(delta):
  18.     # Move in 4 directions
  19.     direction = Input.get_vector("left", "right", "up", "down").normalized()
  20.    
  21.     if direction:
  22.         velocity = direction * speed
  23.     else:
  24.         velocity = Vector2.ZERO
  25.  
  26.     move_and_slide()
  27.  
  28. func update_animation_parameters():
  29.     if(velocity == Vector2.ZERO):
  30.         animation_tree["parameters/conditions/idle"] = true
  31.         animation_tree["parameters/conditions/is_moving"] = false
  32.     else:
  33.         animation_tree["parameters/conditions/idle"] = false
  34.         animation_tree["parameters/conditions/is_moving"] = true
  35.  
  36.     if(Input.is_action_just_pressed("use")):
  37.         animation_tree["parameters/conditions/swing"] = true
  38.     else:
  39.         animation_tree["parameters/conditions/swing"] = false
  40.        
  41.     if(direction != Vector2.ZERO):
  42.         animation_tree["parameters/Idle/blend_position"] = direction
  43.         animation_tree["parameters/Swing/blend_position"] = direction
  44.         animation_tree["parameters/Walk/blend_position"] = direction
  45.    
  46.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement