this post was submitted on 18 Oct 2023
3 points (100.0% liked)

Godot

5884 readers
1 users here now

Welcome to the programming.dev Godot community!

This is a place where you can discuss about anything relating to the Godot game engine. Feel free to ask questions, post tutorials, show off your godot game, etc.

Make sure to follow the Godot CoC while chatting

We have a matrix room that can be used for chatting with other members of the community here

Links

Other Communities

Rules

We have a four strike system in this community where you get warned the first time you break a rule, then given a week ban, then given a year ban, then a permanent ban. Certain actions may bypass this and go straight to permanent ban if severe enough and done with malicious intent

Wormhole

[email protected]

Credits

founded 1 year ago
MODERATORS
 

Hey y'all, throwing a little project together in Godot 4.1 to practice stuff I learned after a tutorial project. I've set up two markers as my projectile spawn points, but the shots are spawning slightly off from the markers and I can't figure out why. Code and pics below. ~~I "fixed" it by shifting the player sprite2D 5 pixels to the right. Not sure why the spawns are being offset though.~~ NVM that only fixed it at the starting position, the shots are way off when rotating. In editor you can see the markers at the two tips on the ship.

In game you can see the shots spawning just to the side of the markers.

Edit: I broke the formatting posting on mobile ๐Ÿ™ƒ

This snippet is from the player script

` var player_direction = (Globals.player_pos - $FixedHardpointDirection.position).normalized()

if Input.is_action_just_pressed("fire_primary") and can_shoot and Globals.laser_ammo > 0 and hardpoint_type == 0:

	Globals.laser_ammo -= 1

	var hardpoint_positions = $ShotStartPositions.get_children()

	can_shoot = false

	$Timers/LaserTimer.start()

	for i in hardpoint_positions:

		player_shot_fixed_weapon.emit(i.global_position, player_direction)

`

And this is from the level script

` func _shoot_fixed_weapon(pos, direction):

var laser = laser_scene.instantiate() as Area2D

laser.position = pos

laser.rotation_degrees = rad_to_deg(Globals.player_rotation)

direction.x = cos(Globals.player_rotation)

direction.y = sin(Globals.player_rotation)

Globals.fixed_hardpoint_direction = Vector2(direction.x,direction.y)

print(Globals.fixed_hardpoint_direction)

laser.direction = Globals.fixed_hardpoint_direction.rotated(-1.5708)

$Projectiles.add_child(laser)

`

top 3 comments
sorted by: hot top controversial new old
[โ€“] TaZ 2 points 1 year ago* (last edited 1 year ago) (1 children)

Is the laser sprite positioned at the origin of the laser scene?

Edit: I see you're giving the global position of the hardpoint into the emitted signal, and the script that spawns the laser instance assigns a value to the local position of the instance. Perhaps that could explain it? In other words: is the origin of $Projectiles at Global 0,0?

[โ€“] daddybutter 3 points 1 year ago (1 children)

Ahhhhh you genius haha... For some reason my level parent node was shifted 5 pixels which was then throwing everything off. It's all lined up now, thank you so much!

[โ€“] TaZ 1 points 1 year ago

You're welcome!