How to rotate moving object over time?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Clicksurfer

Hi there,

After completing the Dodge The Creep tutorial, I figured I’d liven things up by making the spawned mobs rotate over time (so instead of going in a straight line, they’ll make a circular motion).

However, I am unsure what value I need to supply and where in order to make this happen.

The mobs are given their initial velocity using the following logic:

# Set the mob's direction perpendicular to the path direction. var direction = $MobPath/MobSpawnLocation.rotation + PI / 2 # Set the mob's position to a random location. mob.position = $MobPath/MobSpawnLocation.position # Add some randomness to the direction. direction += rand_range(-PI / 4, PI / 4) mob.rotation = direction # Set the velocity (speed & direction). mob.linear_velocity = Vector2(rand_range(mob.min_speed, mob.max_speed), 0) mob.linear_velocity = mob.linear_velocity.rotated(direction)

I thought all I would need to do would be add some angular velocity to get the object to spin:

	mob.angular_velocity = PI

But this only got a weird effect where the object visually rotates but it is still heading in more or less the same direction.

Any ideas? Thanks in advance!

:bust_in_silhouette: Reply From: Lopy

The translation is defined in your code by
mob.linearvelocity = Vector2(randrange(mob.minspeed, mob.maxspeed), 0)
mob.linearvelocity = mob.linear_velocity.rotated(direction).
If you want it to adapt with the rotation, you would need to set it again over time, like this :

func _physics_process(_delta):
. mob.linearvelocity = Vector2(mob.linearvelocity.length(), 0) //set back straight
. mob.linearvelocity = mob.linear_velocity.rotated(mob.rotation) //give the wanted rotation