# Random Rotation

Hello I am new to GDevelop and am starting to make my first game. I know the basics but I have a problem with rotating objects. I have one object rotating clockwise at 100 degrees per second which is what I want. But I have another object that I want to rotate randomly at different speeds and have it at randomly switch from clockwise to counterclockwise and back infinitely. If someone could tell me how to do this it would mean a lot. Thanks.

Something like this will choose a random rotation speed between 50 and 150

``Rotate object2 at speed 50+Random(100) deg/second``

To change direction means making it negative, which is simplest to do by multiplying the above by a number which is randomly negative. The expression Random(1)-0.5 will do this by being negative half the time and positive half the time.
Combined with the speed calculation:

``Rotate object2 at speed (50+Random(100))*(Random(1)-0.5)deg/second``

The problem is that it will change 60 times a second, which is probably too fast to see what is happening, so you’ll have to add in a timer to make the speed/direction only change every x seconds. It is probably best to have the direction and speed stored in an object variable so that different instances of the same object will behave independently. To do this right-click on the object in the objects editor and choose “Other properties”. Then click on “click to edit…” next to “Variables” followed by the “+” icon. You can then type the name of variable e.g. rotspeed

This means two events:

1. one to decide when to change direction/speed and what the new speed/direction will be and put that number into an object variable
2. one to update the object rotation using the value in object2.rotspeed

Event 1

``````The timer "rotchange" is greater than 2 seconds     |   Reset the timer "rotchange"
Do =  (50+Random(100))*(Random(1)-0.5) to variable rotspeed of object2                                                                                                       ``````

Event 2

``No conditions | Rotate object2 at speed object2.Variable(rotspeed)deg/second``

Set up like this you can make as many copies of object2 as you like in the scene editor and they will all rotate with their own random speeds/directions.

1 Like

Thank you this helped me a lot! Hi MattLB,
saw your post and your short & good solution with the (Random(1)-0.5) to generate negative numbers for direction change in rotation.

But the results are in a range form -0.5 to +0.5. That means, you need to multiple whole result by 2.
Otherwise your will get a rotation between (+/-) 25 and 75.