#Character Rotation
1 messages · Page 1 of 1 (latest)
extends CharacterBody2D
signal tank_fired(barrel: Marker2D)
#Tank Movement
const FRICTION: float = 5.0
#Upgradables
var speed: float = 25.0
var acceleration: float = 5.00
var hull_turn_speed: float = 0.01
var turret_turn_speed: float = 0.02
var reload_timer: float = 2.0
@onready var tankTurret: Node2D = get_node("Turret")
@onready var tankBarrel: MeshInstance2D = get_node("Turret/Barrel")
@onready var bulletSpawnPosition: Marker2D = get_node("Turret/BarrelEnd")
@onready var reloadTimer: Timer = get_node("Timer")
func _physics_process(delta: float) -> void:
var turn_direction: float = Input.get_axis("left", "right")
var move_direction: float = Input.get_axis("down", "up")
var direction: Vector2 = Vector2(turn_direction, move_direction)
#Rotates the tank hull based on A/D keys
rotate(turn_direction*hull_turn_speed)
#Rotates the turret to mouse position
#WORKING
#tankTurret.look_at(get_global_mouse_position())
#Turret rotation cap
#not working :(
var angle: float = tankBarrel.global_position.angle_to(get_global_mouse_position())
tankTurret.rotate(sign(angle)*turret_turn_speed)
print(angle)
I'm having an issue with getting the turret to look at the mouse position in some cases
nor am I really sure if i'm doing this the best way
Can you send node tree
So is the issue of the rotation cap not working or the look-at mouse sometimes not working? Cause it looks right in the video
the look_at() is fine and works but it doesn't feel very immersive to what i'm trying to do
so i'm trying to use rotate() to try and slowly rotate the turret to the mouse position and in the video you can see the mouse position and barrel aren't necessarily aligned at certain times when i pause and when i shoot the bullet goes somewhere completely else
i'll send a video give me a quick second
this is the intended zoom i wanted to have for the game and you can see the problems more clearly
ideally i want the bullets to be shooting towards the mouse
Ah
It could be because calling rotate causes it to "over step" on the last frame
Also, it's frame dependent which is another issue
To make it non frame dependent, change tankTurret.rotate(sign(angle)*turret_turn_speed) to tankTurret.rotate(sign(angle)*turret_turn_speed*delta)
That will solve it and reduce the error
the problem with that is it makes the rotation abysmally slow now
o yeah, you just need to increase turret_turn_speed
Rule of thumb: for any physics process, multiply by delta
Otherwise you end up with frame dependent speeds
Not multiplying with delta will make the turret turn twice as fast at 120 fps than if it was in 60 fps for example
it unfortunately still causes the over rotation issue
Yes it will
Cause it's not frame dependent
But it still will overstep on the last frame
There are many ways to fix this depending on how you want the turret to behave
Actually nm
Found the error:
var angle: float = tankBarrel.global_position.angle_to(get_global_mouse_position())
should be
var angle: float = tankTurret.global_position.angle_to(get_global_mouse_position())
You need to check the angle from the center of the rotation axis
When you use the tank barrel, your using the center of the tank barrel
Which is not what you want
You want to use what it's rotating on
which is the turret
idk how to explain it but that doesn't work either
Did it change anything?
when I spawn in and don't move the tankTurret.global_positionstarts off at 0 and doesn't actually go up or down when i'm rotating
it just spins in a circle forever basically
.
last bump
im working on something similar but have a slightly different issue