Projectile is overlapping player and going inwards

Godot Version

3.5.1

Question

Hello there! im creating a top down shooter and have watched a few tutorials to do so, but I ran into an issue. When you click the screen, the bullet is shot fine, but if you click close to the character the bullet does something weird and goes inwards before dissapearing. I have a position 2D node attached to my character to set the position where the projectile is being shot, so my guess is that it has something to do with that.


I have also tried to enable colision with the character through layers and masks but it makes the character move for some reason

here is the function for shooting inside the character script:

const pistol_bullet = preload("res://scenes/Bullet.tscn")

func _process(delta):
	if Input.is_action_just_pressed("disparo"):
		shoot()
	$nodepos.look_at(get_global_mouse_position())

#
func shoot():
	var newbullet = pistol_bullet.instance()
	get_parent().add_child(newbullet)
	newbullet.position = $nodepos/pos.global_position
	newbullet.velocity = get_global_mouse_position() - newbullet.position
	newbullet.look_at(get_global_mouse_position())

and here’s the bullet code:

var velocity : Vector2 = Vector2(0,1)
var speed = 100
var duration = 10

func _physics_process(delta):
	
	var collision = move_and_collide(velocity.normalized() * delta * speed)
	
	if collision != null:
		queue_free()
	duration -= delta
	if duration < 0 :
		queue_free()

any help is appreciated!

This is the issue code.

You need to find a direction first.

Var direction: Vector2 = (get_global_mouse_position() - player.global_position()). normalized()
newbullet.velocity =  direction
newbullet.transform.look_at(direction) # local space look_at

The only thing I made up is the player character global position. I don’t know where it can be accessed.

To some degree you don’t have to manually rotate the bullet if you can leverage the transforms of nodepos & pos node.

1 Like

Thanks! It worked, but I’m having issues with the bullet rotation

onready var player = get_node(".")
func shoot():
	var newbullet = pistol_bullet.instance()
	get_parent().add_child(newbullet)
	newbullet.position = $nodepos/pos.global_position
	var bulletdirection: Vector2 = (get_global_mouse_position() - player.get_global_position()).normalized()
	newbullet.velocity = bulletdirection
	newbullet.transform.look_at(bulletdirection)

in the last line it shows the error nonexistent function “look_at” in base “transform2D”
I tried keping it as newbullet.look_at(bulletdirection) but the rotations are set only to the place where the character spawns, when it moves to another location it doesn’t work

Sorry, try looking_at

Try it like this

newbullet.transform = Transform2D.looking_at(direction)

static constant ‘looking_at’ not present in built-in type Transform2D
do u have any idea why is this happening? :confused:

I apologize again, that’s a Godot 4 function.

Let’s try using the look_at as before and we will use direction, but will also need to add a global position.

newbullet.look_at(direction + get_global_mouse_position())

That works better, but when I click on the character to shoot, the sprite of the bullet is flipped horizontally

However I found out that newbullet.rotation = bulletdirection.angle() works just as I wanted

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