so how can I do this, but make it more modoal so I can use it for npcs too, can't use…
- User
so how can I do this, but make it more modoal so I can use it for npcs too, can't use mouse position but still want the rotation to be towards the way it gets shooted:
extends Node2D
var controller = null var mana_cost = 0
Called when the node enters the scene tree for the first time.
func _ready(): pass # Replace with function body.
Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta): pass
func execute(s): controller = s var is_able_to_cast = controller.can_cast_ability(mana_cost) if !is_able_to_cast: return
var f = load("res://Scenes/abilities/swordslash/swordslash_wave.tscn") var f_node = f.instantiate() f_node.controller = controller var hand_node = controller.get_node("Hand") # Adjust this if ‘Hand’ is more deeply nested if not hand_node: print("Error: Hand node not found in the controller.") return f_node.position = hand_node.global_position var mouse_position : Vector2 = (get_global_mouse_position() - global_position).normalized() f_node.rotation = mouse_position.angle() f_node.add_collision_exception_with(controller) get_node("/root").add_child(f_node) - Assistant
If you want to make your code more modular and be able to use it for NPCs without relying on the mouse position, you could pass in a target position or direction instead. Here's an updated script with changes to accomplish this:
extends Node2D var controller = null var mana_cost = 0 func _ready(): pass func _process(delta): pass func execute(s, target_direction = Vector2(1, 0)): controller = s var is_able_to_cast = controller.can_cast_ability(mana_cost) if !is_able_to_cast: return var f = load("res://Scenes/abilities/swordslash/swordslash_wave.tscn") var f_node = f.instantiate() f_node.controller = controller var hand_node = controller.get_node("Hand") # Adjust this if 'Hand' is more deeply nested if not hand_node: print("Error: Hand node not found in the controller.") return f_node.position = hand_node.global_position f_node.rotation = target_direction.angle() f_node.add_collision_exception_with(controller) get_node("/root").add_child(f_node) - User
help me rewrite this so it is smoother and also looks better:
func _read_input(): var mouse_position : Vector2 = (get_global_mouse_position() - global_position).normalized() if mouse_position.x > 0 and animator.flip_h: animator.flip_h = false elif mouse_position.x < 0 and not animator.flip_h: animator.flip_h = true var movement = []
if Input.is_action_pressed("move_up"): movement.append("up") state = State.RUN if Input.is_action_pressed("move_down"): movement.append("down") state = State.RUN if Input.is_action_pressed("move_right"): movement.append("right") state = State.RUN if Input.is_action_pressed("move_left"): movement.append("left") state = State.RUN move.execute(self, movement) if last_ability > global_cooldown: if Input.is_action_just_pressed("interact"): interact() last_ability = 0 if Input.is_action_just_pressed("SwapPlane"): _on_swapPlane() last_ability = 0 #if Input.is_action_just_pressed("shoot"): # perform_attack() if Input.is_action_pressed("shoot"): attack_animation = "Attack" + str(randi_range(1, 2)) animator.play(attack_animation) swordslash.execute(self, mouse_position) last_ability = 0 await animator.animation_looped #if Input.is_action_just_pressed("inventory"): # inventory.execute(self) # last_ability = 0 if state == State.RUN: animator.play("Run") else: animator.play("Idle") state = State.IDLE - Assistant
- User
don't remake the move.execute, only fix with the states and animations
- Assistant
- User
now it stops the attack animation imediatly
- Assistant