Attempting to get var of rotated sprite to another

Godot Version

4.3

Question:

I am newer to Godot, and I’m attempting to get a variable from my gun to my player, so I can have my player play a player animation that corresponds with where the gun is pointing. heres the code im working with for both my gun and my player. I did my best to try to find it out but I had no success :frowning:

Player:

extends CharacterBody2D
> 
> 
> ## Movement-------------------------------------------------------------------
> 
> ## variables for all movement
> const SPEED = 300.0
> const Dash = 500
> 
> ## using this for where the gun is looking at
> const GunPosition = preload("res://Player/Weapon/gun.tscn")
> 
> ## walking----------------------
> 
> 
> func get_input():
> 	var input_direction = Input.get_vector("left", "right", "up", "down")
> 	velocity = input_direction * SPEED
> 
> func get_direction():
> 	rotation_degrees = wrap(GunPosition.rotation_degrees, 0, 360)
> 	if rotation_degrees > 90 and rotation_degrees < 270:
> 		## i doubt this is the correct direction, this is here for testing purposes
> 		$AnimatedSprite2D.play("down-idle")
> 	
> func _physics_process(_delta):
> 	get_direction()
> 	get_input()
> 	move_and_slide()

###gun

> extends Node2D
> 
> const Bullet = preload("res://Player/Weapon/Bullet.tscn")
> 
> @onready var muzzle: Marker2D = $Marker2D
> 
> 
> func _process(_delta: float) -> void:
> 	rotation_degrees = wrap(rotation_degrees, 0, 360)
> 	look_at(get_global_mouse_position())
> 
> 	if Input.is_action_just_pressed('Shoot'):
> 		var Bulletinst = Bullet.instantiate()
> 		get_tree().root.add_child(Bulletinst)
> 		Bulletinst.global_position = muzzle.global_position
> 		Bulletinst.rotation = rotation

please properly format your code with the </> button, and make sure there are tabs.
tabs are very much important.

I have updated the post, thanks for telling me :+1:

The scene you are preloading is like a blueprint, it’s not an actual gun, it’s how to make a gun. I’m betting your gun is already a child of the player, you can use the dollar sign or get_node to get children of the current script.

@onready var gun = $Gun

func get_direction():
	var gun_rotation_degrees = wrap(gun.rotation_degrees, 0, 360)
	if gun_rotation_degrees > 90 and gun_rotation_degrees < 270:
		## i doubt this is the correct direction, this is here for testing purposes
		$AnimatedSprite2D.play("down-idle")

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.