# Can't see why my code wouldn't work

**URL:** <https://forum.godotengine.org/t/cant-see-why-my-code-wouldnt-work/12669>\
**Category:** Archive\
**Created:** [February 23, 2021, 9:27am UTC](https://forum.godotengine.org/t/cant-see-why-my-code-wouldnt-work/12669 "2021-02-23T09:27:29Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![system](https://forum.godotengine.org/uploads/default/original/2X/4/4b0a7fa8a6334634b94a13d421f0d99e6f04e21f.svg) [@system](https://forum.godotengine.org/u/system)\
**Post date:** [February 23, 2021, 9:27am UTC](https://forum.godotengine.org/t/cant-see-why-my-code-wouldnt-work/12669/1 "2021-02-23T09:27:29Z")

</div>

| | | |
| --- | --- | --- |
| ℹ | **Attention** | Topic was automatically imported from the old Question2Answer platform. |
| 👤 | **Asked&nbsp;By** | Elayon |

It’s supposed to detect which direction the mouse is headed to from the players perspective and get a gun out from that direction. I’ve been trying so hard to fix it. I’d also really appreciate any tips on how could I improve this code.

```auto
if Input.is_action_pressed("MouseClick"):
	
	var mousePos = get_local_mouse_position()
	
	var direction : String
	
	if $Weapon.offset.y == 0:
		
		if abs(mousePos.x) - abs(mousePos.y) >= 0:
			
			if mousePos.x >= 0:
				
				direction = "right"
				$Weapon.rotation_degrees = 90
				
			else:
				
				direction = "left"
				$Weapon.rotation_degrees = -90
				
			
		else:
			
			if mousePos.y <= 0:
				
				direction = "top"
				$Weapon.rotation_degrees = 0
				
			else:
				
				direction = "bottom"
				$Weapon.rotation_degrees = 180
			
		
	
	while Input.is_action_pressed("MouseClick") and direction == get_direction_changed():
		print(direction)
		if $Weapon.offset.y >= -188:
			
			$Weapon.offset.y -= 4
			
			$WeaponGetOut.start(0.1)
			yield($WeaponGetOut, "timeout")
			
		else:
			
			$Weapon.play("MinigunSpin")
			
			if $Weapon.speed_scale < 60:
				
				$Weapon.speed_scale += 0.05
				$WeaponGetOut.start(0.1)
				yield($WeaponGetOut, "timeout")
	
	while (!Input.is_action_pressed("MouseClick") or direction != get_direction_changed()) and $Weapon.offset.y != 0:
		
		get_back_in()
		$WeaponGetOut.start(0.1)
		yield($WeaponGetOut, "timeout")
	
	
else:
	
	get_back_in()
	$WeaponGetOut.start(0.1)
	yield($WeaponGetOut, "timeout")
	
	

```

func get\_back\_in():

```
if $Weapon.speed_scale > 3:
	
	$Weapon.speed_scale -= 0.1
	
else:
	
	$Weapon.playing = false
	
	if $Weapon.offset.y != 0:
		
		$Weapon.offset.y += 4

```

func get\_direction\_changed():

```
var mousePos = get_local_mouse_position()

if abs(mousePos.x) - abs(mousePos.y) >= 0:
	
	if mousePos.x >= 0:
		
		return "right"
		
	else:
		
		return "left"
		
		
	
else:
		
		if mousePos.y <= 0:
			
			return "top"
			
		else:
			
			return "bottom"

```

Sorry, could you say what the result you’re getting right now is? That would make it a lot easier to answer.

exuin | 2021-02-23 16:23

---

<div class="post-metadata">

**Author:** ![system](https://forum.godotengine.org/uploads/default/original/2X/4/4b0a7fa8a6334634b94a13d421f0d99e6f04e21f.svg) [@system](https://forum.godotengine.org/u/system)\
**Post date:** [February 24, 2021, 6:35am UTC](https://forum.godotengine.org/t/cant-see-why-my-code-wouldnt-work/12669/2 "2021-02-24T06:35:01Z")

</div>

| | | |
| --- | --- | --- |
| 👤 | **Reply From:** | Whalesstate |

i checked it fast and it could be implemented easier than this , you can replace the order of mouse\_dir values if it’s inverted

```auto
var player := KinematicBody2D.new()
var gun := Sprite.new()
func _process(_delta : float) -> void:
	var mouse_dir = get_global_mouse_position() - player.global_position
	gun.look_at(mouse_dir)

```
