How can I make my gun aiming at the mouse, and my gun always facing the mouse?

Player code:

extends CharacterBody2D


const SPEED = 300.0
const JUMP_VELOCITY = -300.0

# Get the gravity from the project settings to be synced with RigidBody nodes.
var gravity = ProjectSettings.get_setting("physics/2d/default_gravity")

@onready var animated_sprite = $AnimatedSprite2D
@onready var gun = $Gun

func _physics_process(delta):
	
	#Shoot.
	if Input.is_action_pressed("shoot"):
		gun.shoot()
	
	# Add the gravity.
	if not is_on_floor():
		velocity.y += gravity * delta

	# Handle jump.
	if Input.is_action_just_pressed("jump") and is_on_floor():
		velocity.y = JUMP_VELOCITY

	# Get the input direction and handle the movement/deceleration.
	var direction = Input.get_axis("left", "right")
	
	# Sprite flip.
	if direction > 0:
		animated_sprite.flip_h = false
	elif direction < 0:
		animated_sprite.flip_h = true
	
	if direction:
		velocity.x = direction * SPEED
	else:
		velocity.x = move_toward(velocity.x, 0, SPEED)

	move_and_slide()

Gun’s code

extends Node2D

@export var shootSpeed = 1.0

const BULLET = preload("res://scene/bullet.tscn")

@onready var marker_2d = $Marker2D
@onready var shoot_speed_timer = $shootSpeedTimer

var canShoot = true
var bulletDirection = Vector2(1, 0)

func _ready():
	shoot_speed_timer.wait_time = 0.2 / shootSpeed
	

func shoot():
	if canShoot:
		canShoot = false
		shoot_speed_timer.start()
		
		var bulletNode = BULLET.instantiate()
		
		bulletNode.set_direction(bulletDirection)
		get_tree().root.add_child(bulletNode)
		bulletNode.global_position = marker_2d.global_position
		

func _on_shoot_speed_timer_timeout():
	canShoot = true

Surely, Im very very new to coding, I just learned HTML and CSS, and going into these type of coding just blew my mind. I searched all over youtube and trying like 4 different ways of shooting projectile, this one did the job but the creator built it for top-down 2d game, while I want to adapt it for sidescroller, platformer type of game. I think I need to make my player’s sprite to face my mouse to yet still have the ability to walk… backward? I dont even know what/how do you call that, any help will be appreciated, thank you.

I use the following:

look_at(get_global_mouse_position())
1 Like

sorry but where should i place it? or how should i code it

Add a script to your gun, and just place that single line in the physic process of the gun or what ever you want to always point at the mouse…

func _physics_process(_delta: float) -> void:

	look_at(get_global_mouse_position())
1 Like

Lovely, but the bullet wont follow, any idea?

here’a the bullet’s code:

extends Area2D

@export var SPEED = 800
@export var damage = 30

var direction:Vector2

func _ready():
	await get_tree().create_timer(3).timeout
	queue_free()

func set_direction(bulletDirection):
	direction = bulletDirection
	rotation_degrees = rad_to_deg(global_position.angle_to_point(global_position + direction))
	

func _physics_process(delta):
	global_position += direction * SPEED * delta
	

func _on_body_entered(body):
	if body.is_in_group("enemies"):
		body.get_damage(damage)
		queue_free()

You need to match the rotation of the bullet to the gun and have a emittance point at the tip of the gun where you want the bullet to appear.

The below tutorial cover all of this, just watch the section on bullets if you don’t want to watch everything.

1 Like