Turret Firing Problem?

Godot Version

4.3

Question

Hello all,

With a lot of help I am getting really close to solving this problem of controlling my top down tank and movement with the Analog sticks only, ditching the mouse and keyboard controls entirely which though cool were … erm, problematic to say the least.

Now I have this code:

func get_input():
	var input_direction = Input.get_vector("c_left","c_right","c_up","c_down");
	var turret_direction = Input.get_vector("r_left","r_right","r_up","r_down");
	
	velocity = input_direction * speed;
	
	#try and change turrent rotation
	if(turret_direction):
		print("turret direction: " +str(turret_direction) );
		#to change turret_sprite rotation - works
		turret_sprite.rotation = turret_direction.angle() - deg_to_rad(90);
		#how to change turret rotation to fire in the correct direction
		##this line causes the problem##
		#turret_pivot_point.rotation = turret_direction.angle();

	if (input_direction):
		print("Input Direction: " + str(input_direction)) ; 
		tank_sprite.rotation 	= input_direction.angle() + deg_to_rad(tank_offset);
		#to get the spawn point to the correct rotation 
		#turret_pivot_point.rotation = tank_sprite.rotation - deg_to_rad(90); 
		tank_uncombined.rotation = input_direction.angle() + deg_to_rad(tank_offset);
		#tank_combined_dark.rotation = input_direction.angle() + deg_to_rad(tank_offset);
	#end if	
	
	#end

Now, this configuration of code allows the tank to move in the correct direction with the left analog stick AND allows control of the turret sprite with the right. However, I cannot get the Turret_pivot_point the 2D node object to rotate properly around the outside of the turret sprite to fire in the correct direction. That was supposed to be handled by the line under: ##this line causes the problem##

Not only does it not rotate properly getting out of alignment very quickly it also causes the tank turret_sprite rotation to act weirdly as well.

So, there is some math to be done here or working out the vectors but it a bit beyond me at the moment, so, any help would be appreciated.

Thankyou and Regards.

1 Like

The problem might again be with your node hierarchy and pivot alignments.

Does this graphic help showing the node:

Can you show a video of the behavior?

Sure I can but it will take a while! I hate using OBS, it give me grief every time I use it.

I will post back when it is up on YouTube.

Sorry about the time it took to get the video up, had to learn to use tools on another online free video editor, because the last one I used didn’t work anymore.

Video link: https://youtu.be/9lhuLAxTK8g
Filename: turret - turret_rotation.mp4

Rotate turret’s pivot point (node Turret) instead of just its sprite. That way the spawn point will rotate around it as well.

2 Likes

That simple huh!

In all that testing that idea never once popped into my head, and, I am always trying the weird ideas :grinning_face_with_smiling_eyes:. I really thought I would need some sort of vector math with a normalization at the end - boy was I way off base :grin:.

But thankyou nonetheless, it is now working as expected :smiling_face_with_sunglasses:.

:man_bowing:

1 Like