#Camera jittering

1 messages · Page 1 of 1 (latest)

meager musk
#

Well, your camera movement seems fine.
Are you sure that it's not the player which's jittering?

#

Camera jittering

#

I'm not.

tiny slate
#

I'm not entirely sure, though I wouldn't be surprised. I get several suggestions involving changing things about the camera but nothing ever works.

meager musk
#

This line of code is executed in the FixedUpdate method.
You're using Time.deltaTime, which should be used in the Update, so you'll have to change it to Time.fixedDeltaTime.

rb.MovePosition(transform.position + transform.TransformDirection(Xm) * speed * Time.deltaTime);
tiny slate
#

Ill try it out and let you know

#

Its still happening, but to a lesser degree than before

meager musk
#

It's still happening after you've changed Time.deltaTime to Time.fixedDeltaTime?

tiny slate
#

Yea, it is

meager musk
#

Is the jittering happening because of the position or rotation?

tiny slate
#

Movement, I can stand still and rotate just fine. Theres jittering when just moving side to side, but its at its worse when moving & rotating

meager musk
#

Moving the character in the FixedUpdate and the camera in the LateUpdate should be fine

#

I wonder whether the issue may happen because of camera logic being split into 2 separate scripts

tiny slate
#

I already fixed that, its still happening

meager musk
#

Could you give the current code of the camera movement then?

tiny slate
#

Aye aye captain

meager musk
#

Alright, what if you put both rotation and position in LateUpdate?

#

Also, how do you even assign the cameraPos?

tiny slate
#

Ill do my best to explain it:

I have my PlayerModel GameObject, its just a standard capsule with a rigidbody. It has two child empty GameObjects, one is Orientation which is used for rotation, the other is CameraPos which is used for moving the camera.

Then separate from all that, I have an empty game object called CameraHolder with the main camera being a child of that. The main camera is what has the script that I had sent. Then I can reference the CameraPos game object in the CameraControls script and voila, here we are.

#

I know its convoluted and half of it makes no sense, but i'm still very new and learning.

#

(Suggestions are appreciated)

meager musk
#

Suggestions, well, there are, but they're currently not as important as your issue

#

I really do wonder why that's happening

#

I'll try to create a 3D project and check it out quickly

tiny slate
#

Okay, no problem. If you want, I can just send a quick clip showing the GameObjects and their children just so you can get it exactly the way I have it

meager musk
#

alright

tiny slate
meager musk
#

Now, I have copied your skills on my objects, but I cannot really tell whether it's jittering or not

#

I feel like, maybe, but it I cannot really sense it too good

#

Guess it's not really jittering right now

#

Have you put both position and rotation assignment in the LateUpdate?

void LateUpdate()
{
    float mx = Input.GetAxisRaw("Mouse X") * sensX; // Gets mouse input on the x axis and multiplies it by the sensitivity we choose. View notes for Time.deltaTime.
    float my = Input.GetAxisRaw("Mouse Y") * sensY; // Gets mouse input on the y axis and multiplies it by the sensitivity we choose. View notes for Time.deltaTime.

    Yrot += mx;
    Xrot -= my;

    Xrot = Mathf.Clamp(Xrot, -90f, 90f); // Camera cannot look pask your feet and directly above you.

    transform.rotation = Quaternion.Euler(Xrot, Yrot, 0); // Rotates camera on both axis.
    or.rotation = Quaternion.Euler(0, Yrot, 0); // Rotates player on y axis


    transform.position = cameraPos.position;
}
tiny slate
#

No, sorry, I forgot you had said that, lemme give it a shot

meager musk
#

Alright.

tiny slate
#

I tried it, its still there

meager musk
#

Alright, my my head hurts a bit and I cannot really tell whether it's jittering here. Can you?

tiny slate
#

That looks so much better than mine. If there is one, I cant even tell

meager musk
#

Really? I see

#

The code really does seem fine, so..

#

I wonder what in your game could cause that

#

You have just those 2 scripts and our hierarchy looks quite the same

tiny slate
#

I'm just as confused as you are

meager musk
#

I had a look, but I don't seem to find anything

#

Is there any way for you to send me the project for me to take a look at it directly?

tiny slate
#

Ill try

meager musk
#

I'm sorry, I'll have to go now. I'll be able to help you tomorrow, which is in 9 hours for me, if you don't solve this issue till then

tiny slate
#

Thats around 2 am for me. I'm well asleep by then. Are you free five hours after?

meager musk
#

Alright, I am free right now

#

Now, could you, please, send your project in a zip file?

#

It makes no sense for me how your jittering happens

#

There shouldn't be any issues, well..

tiny slate
#

Yeah, ill send it over

#

Sorry im late, I just woke up

tiny slate
#

Gotta do it this way or else the file is too large to send over disc

meager musk
#

well, you're still able to send it using zip

#

It makes the file much smaller, so there shouldn't be a problem

tiny slate
#

I tried sending the zip file, its still too large, sorry

meager musk
#

alright

meager musk
#

So, I was gone for quite a while and now opened your project

#

You see the jittering too, right?

tiny slate
#

Yeah, strange, right?

meager musk
#

Yes, exactly

#

It doesn't make any sense

#

The code doesn't seem right, but it also doesn't seem to be causing this issue

#

Also it's, I guess, working in my environment with the same scripts, but not in your

tiny slate
#

Could it be something to do with the project settings?

#

But idk what it would even be in there

meager musk
#

Yes, it could

tiny slate
#

Do you have and idea as to what it could be? Because im lost

meager musk
#

No, I don't

#

I'll have to take a look at it

hardy path
#

Gonna reply here.
Try moving your rb via velocity instead of MovePosition

meager musk
#

The issue is very strange. Perhaps I'm a bit blind, but I cannot really tell whether it's lagging or not

#

I tested it out yesterday and it seemed to lag

#

Now I fell like it's ok

#

Although, I haven't really changed anything

meager musk
#

And I cannot really see any lags in it

#

Now, I suppose, I could see some jittering before.

#

I reloaded the pc and it doesn't really happen now

#

So you may try this if you haven't tried that yet

#

Because the code seems completely fine...

tiny slate
tiny slate
hardy path
tiny slate
#
    {
        Vector3 Xm = new Vector3(Input.GetAxisRaw("Horizontal"), 0, Input.GetAxisRaw("Vertical")).normalized; 
                                                                                                                  
         rb.velocity = transform.position + transform.TransformDirection(Xm) * speed * Time.fixedDeltaTime;
         
    }```
#

I fly upward because my model is positioned at a y position of 2 off the start so my transform.position of y = 2 is being added to the vector where y is 0 and then its multiplied. That makes sense. However, when I set that to 0, I would no longer fly upward, but if I pressed any of my movement keys, I would fly uncontrollably in the direction of the key I pressed after a few seconds.

#

@hardy path

(Forgot to reply to your message)

hardy path
#
  1. You should not query input in fixed update. It would skip over input sometimes.
tiny slate
hardy path
#

Most physics related should go into fixed update, but input is not physics related.

hardy path
tiny slate
#

Scratch that again, I can move in the direction my camera Is facing, but now I'm having other camera issues

#

I think it's actually the player movement that’s causing the issue. I’m having an issue right now where my camera is detached from the player and rotating on its own, but I can still move my model. I can shove over my rest objects and rotate the camera and they appear fine.

#

That is unlike what happens when I’m actually in first person

hardy path
hardy path
tiny slate
#

I’m at school right now, I can send a clip when I get home if you want so you can give your thoughts.

meager musk
#

I currently don't see any changes from moving it with MovePosition, as they both should not cause the jittering, but you can indeed use it if it fixes the issue for you

meager musk
# tiny slate ```private void FixedUpdate() { Vector3 Xm = new Vector3(Input.GetA...

In order to get the velocity, you should multiply the direction by the speed, where direction is transformed from local, in your case Xm, to world coordinates. You do it correctly.

The issue in the code you shared is that you

    1. Add the transform.position, as the player's position is always changed and it doesn't make any sense to increase or decrease the velocity according to it
    1. Multiply it by Time.fixedDeltaTime, which is a constant value, unless is changed by you, found in the project settings. This is redundant for you and will just make the velocity either greater or smaller, when the value is changed to either greater or smaller respectively
tiny slate
hardy path
tiny slate
hardy path
tiny slate
hardy path
# tiny slate One: I dont quite understand the first sentence in what you said. Whats wrong wi...
  1. It's not wrong to rotate the rb, if you do it in a physics compatible way. If it's not in a physics compatible way, that would break interpolation and cause jitter.
    Physics compatible way is what I listed, setting angular velocity, applying torque or using MoveRotation on the rb. Anything else is not physics compatible, as it overrides or goes against the physics calculations.
  2. So it sounds like you're rotating your rb via transform, which is not a physics compatible way. So it's likely the cause of the issue.
tiny slate
#

Oh, okay, that makes a lot more sense, thank you!

I was reading the docs on the three ways you had mentioned, i'll give MoveRotation a shot. Thank you!

tiny slate
#

@hardy path

I'm so sorry, but I need one more thing. You were correct, it did and did not fix my issue.

Using MoveRotation fixed the issue, but to do it, I had to comment out the part of my camera controls script that rotates the player on the y axis. I can no longer move in the direction my camera is looking. But if I put it back in, the jitter persists.

#

Scratch that, its not just that, it appears that im also literally just moving in circles. I hold down W for example, and I go forward for a second then do a full circle back to where I was. All of my movement keys do this

hardy path
tiny slate
hardy path
tiny slate
#

Actually, the line or.rotation = Quaternion.Euler(0, Yrot, 0); is what fucked up everything. Just out curiosity, I commented out everying MoveRotation related and the jitter was still gone. So basically, I just need a way to move in the direction im facing that is physics compatible.

hardy path
#

You'll need to share the latest code if you need further help.

tiny slate
#

Sorry to bug you so much, I do really appreciate the help though.

hardy path
tiny slate
#

Or rather, if there is, I don't see anything different if its there versus if there isn't

hardy path
tiny slate
#

Why is it incorrect? I genuinely don't know; I was just following the docs, which is sometimes a bad practice, but I just figured it was safe.

hardy path
#

What docs exactly do that?

tiny slate
#

The move rotation docs do all the same 3 errors you pointed out to me

hardy path
#

It makes sense in their specific case, because they multiply by a delta rotation

#

in your case you want your rb rotation to be equal to your final rotation

#

it's a purely a logic issue. You don't even need to know programming to see an issue there.

tiny slate
#

One: How would I do that?

Two: I'm dumb as a box of crayons and not the most logical so that tracks

hardy path
#

Just use the final rotation in MoveRotation. No need to multiply it by anything

tiny slate
#

Okay, yeah, I understand that.

My issue of walking in constant circles still persists though. Any thoughts on that?

#

Hell, actually, now that I take another look, I dont walk in circles when I playtest. I'm still not moving where my camera is looking, but im also not moving in circles. It takes about 30 seconds before that happens

hardy path
#

Also, a video of the issue would be helpful

tiny slate
#

Yeah, I can do that, just give me a second to get it all.

How would I share the hierarchy/inspector setup, would the video work or is there something else that I just dont know of?

hardy path
tiny slate
#

Turns out actually, that bumping into one of my objects has my y rotation on my player model increase and it keeps increasing, then it goes into the negatives then back into the positives. Its starts constantly fluctuating when I hit an object

#

Nevermind, I figured it out. The line where I had or.rotation = Quaternion.Euler(0, Yrot, 0); was causing my problem. So if what I needed to do was rotate my player in a physics-compatible way, then I should set the rotation of my player model equal to my final rotation in the fixed update.

#

I really appreciate the help you've given me, I don't like moving on to a different task without fixing a problem and this is a problem that has discouraged me for a while. I also learned more about Unity in general instead of just tunnel visioning on this. Thanks a ton!

#

Or rather ill just go screw myself. I got curious and tested something, turns out, all I needed to do was move or.rotation = Quaternion.Euler(0, Yrot, 0); into FixedUpdate instead of LateUpdate and it works the same. Not a problem though, at least I know now, and besides, I can probably use the rest of what I learned from you later on. Thanks again!

hardy path
#

I guess that means that the issue is fixed..?