# Automoving 3D game issue

Hello.

I am building a 3D game where the character moves automatically forward.

If the game remain a long straight line, it gets annoying and so not enjoyable. To get it ‘funnier’ I wanted to add a curved path but I can’t get my character to turn on let’s say 90°, at the same speed and progressively. It always turns instantly like an L and not a C. Also when it happens, from what I understood I am having a grimbal lock bug.

After some researches I think I have to use quaternions but I ain’t sure and can’t find a similar script (not to copy it but to understand how it works) on the Internet.

How am I supposed to do, and what am I supposed to use to make it turn like a C while using an autorunning forward script ?

If you need parts of my code, tell me and I will update this post.

Btw, I code in c#.

What’s the code for turning your object?

I deleted it as it was buggy. But I was using a couroutine like transform.Rotate (0, 90, 0);

Now I don’t have anything to make it turn

I doubt it has anything to do with Gimbal lock. The reason it turns 90 degrees instantly is because that’s exactly what `transform.Rotate (0, 90, 0);` does.

What you actually need to do is rotate a small amount over the course of many iterations. Use Time.deltaTime each frame to compute an increment.

What made me think of the gimbal lock is that my character was going forward, turning 90°, going forward again etc once the transform.rotate was called.

Anyway. Where am I supposed to place Time.deltaTime ? I don’t get how to use it. I’m pretty new to this, might need to read a documentation or something about it.

Time.deltaTime = time in seconds to complete the last frame
And your Update() method is called each frame.

So if you rotate your character in your update method like this

``````private void Update() {
transform.Rotate(0, 90, 0);
}
``````

…it rotates 90 degrees each frame, which results in whopping 5400 degrees per second (Edit: if the game runs with 60 fps)

If you instead multiply it with Time.deltaTime like this

``````private void Update() {
transform.Rotate(0, 90 * Time.deltaTime, 0);
}
``````

… it “only” rotates 90 degrees per second (still too much i think but that’s the use of it)

1 Like

Imagine, if you are running at 60 fps and you want to rotate 90 degrees, and you want it to take 1 second then what needs to happen is that 90 degrees needs to be divided between the 60 frames. And you rotate a little each frame.

Time.deltaTime is the amount of time that passes between each frame (in seconds). for example, if you you’re running at exactly 60 frames per second, then Time.deltaTime will be (1/60) which is 0.01666… in decimal. You can’t really count on what your FPS will be, so use Time.deltaTime, rather than a specific number.

A formula for the amount you should rotate each frame is: ((full rotation amount) / (amount of time it should take)) * Time.deltaTime. For example, if want to rotate 90 degrees over the course of two seconds than you want to

`````` rotate_amount = (90f/2f)*Time.deltaTime;
``````

and keep doing that until you rotate the full amount. But how do you know when you’re done? You can declare a float called amount_remaining=90 and subtract rotate_amount each time until you are =< 0.

1 Like

Thanks for the explanations. You really helped.

Thanks for your explanations too, you both really helped out.