#Character Rotation

1 messages · Page 1 of 1 (latest)

olive flame
#

I would like to rotate my "turret" just like a tank where there is some time it takes for the tank to rotate until it reaches the mouse position

#
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

night shoal
#

Can you send node tree

olive flame
night shoal
olive flame
#

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

night shoal
#

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

olive flame
#

the problem with that is it makes the rotation abysmally slow now

night shoal
#

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

olive flame
#

it unfortunately still causes the over rotation issue

night shoal
#

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

olive flame
#

idk how to explain it but that doesn't work either

night shoal
#

Did it change anything?

olive flame
#

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

olive flame
olive flame
#

.

olive flame
#

last bump

ocean pike
#

im working on something similar but have a slightly different issue