#Camera jittering
1 messages · Page 1 of 1 (latest)
Well, your camera movement seems fine.
Are you sure that it's not the player which's jittering?
Camera jittering
I'm not.
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.
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);
Ill try it out and let you know
Its still happening, but to a lesser degree than before
It's still happening after you've changed Time.deltaTime to Time.fixedDeltaTime?
Yea, it is
Is the jittering happening because of the position or rotation?
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
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
I already fixed that, its still happening
Could you give the current code of the camera movement then?
Alright, what if you put both rotation and position in LateUpdate?
Also, how do you even assign the cameraPos?
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)
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
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
alright
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;
}
No, sorry, I forgot you had said that, lemme give it a shot
Alright.
I tried it, its still there
Alright, my my head hurts a bit and I cannot really tell whether it's jittering here. Can you?
That looks so much better than mine. If there is one, I cant even tell
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
I'm just as confused as you are
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?
Ill try
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
Thats around 2 am for me. I'm well asleep by then. Are you free five hours after?
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..
well, you're still able to send it using zip
It makes the file much smaller, so there shouldn't be a problem
I tried sending the zip file, its still too large, sorry
alright
So, I was gone for quite a while and now opened your project
You see the jittering too, right?
Yeah, strange, right?
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
Could it be something to do with the project settings?
But idk what it would even be in there
Yes, it could
Do you have and idea as to what it could be? Because im lost
Gonna reply here.
Try moving your rb via velocity instead of MovePosition
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
That's the video I just got from this project you've sent
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...
Sorry, I did not see this yesterday, how would I do that? I just tried to, but instead of movement working as intended, I just float straight upwards at an exponential rate.
I tried that, it didn't work
That doesn't sound correct. Maybe that could provide a clue to the issue. Share the code that you tried
{
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)
- Velocity is the amount and direction an object would move over time. It doesn't make sense to include the object position in the calculation.
- You should not query input in fixed update. It would skip over input sometimes.
So how would you change it to fix these?
Also, in the spirit of learning something new, I thought anything physics-related should go into fixed updates. Why is that not the case here?
Most physics related should go into fixed update, but input is not physics related.
As for the fix, query the input in a regular update, cache the values so that you can use them in fixed update.
And fix your velocity formula. Velocity is direction*magnitude(speed), I'm sure you've learned it at school.
What do you mean? Unity does it in their docs.
https://docs.unity3d.com/ScriptReference/Rigidbody.MovePosition.html
So, good and bad news. The good news, is that the jittering has decreased significantly. The bad news, is that I can no longer move I the direction my camera is facing.
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
Yeah, that's an example of when you shouldn't follow the docs.
Although, since it's querying the axis, the impact is probably minimal.
It would be more significant if it was GetKey or something similar, that is only active for one frame.
Yeah, it is the character movement, as your camera doesn't move. Though, the rotation could be relevant to the issue, depending on what rotates.
I’m at school right now, I can send a clip when I get home if you want so you can give your thoughts.
That's because you're performing the calculations wrong, yet I cannot tell what's wrong without you sharing the code.
The movement with velocity, mentioned by dlich, should be performed this way.
Vector3 input = new(Input.GetAxisRaw("Horizontal"), 0f, Input.GetAxisRaw("Vertical"));
rb.velocity = speed * transform.TransformDirection(input);
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
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
-
- Add the
transform.position, as the player'spositionis always changed and it doesn't make any sense to increase or decrease thevelocityaccording to it
- Add the
-
- 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 thevelocityeither greater or smaller, when the value is changed to either greater or smaller respectively
- Multiply it by
I think you're onto something with the rotation.
The movement is fixed, I can move side to side now with no jitter. I can also stand still and rotate with no jitter. However, if I move AND rotate, I still get the jitter. Considering the movement is now fixed, im more inclined to say this is now a rotation problem
I thought that you don't rotate the rb, but then there shouldn't be any jitter. If you do rotate it, you should do it in a physics compatible way, like torque, angular velocity or MoveRotation, otherwise it would break the interpolation and cause jitter.
Sorry, I'm not entirely sure what you mean, it's not you, I'm just still very new to all this. Can I send you my current code and you explain what you mean?
Player Movement : https://gdl.space/uquqowaxav.cpp
Camera : https://gdl.space/zukiranaxe.cpp
What exactly do you have trouble understanding?
Also, it's more of a setup issue, rather than code itself.
For example, what is assigned to the or field in CameraControls script?
One: I dont quite understand the first sentence in what you said. Whats wrong with rotating the rb, also, what makes what im doing not physics compatible?
Two: My player model is assigned to or in the camera controls script so that I can move in the direction im facing. That variable was a little misleading so I gotta change that.
- 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. - 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.
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!
@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.
New Cam Controls Script : https://gdl.space/iwicicolay.cs
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
Well, that code is all over the place, so no wonder.
- You use Rot in MoveRotation, but you never set it after start.
- You multiply the final rotation by delta time for some reason. That doesn't make sense.
- You multiply rb rotation by final rotation which doesn't make sense either.
1 : That ones my bad, I dont know why I did that.
2/3: I was honestly just following the docs, thats my bad.
Beyond that, you got any suggestions for whats currently going on?
Does it still not work after fixing the issues?
No, I'm still just moving in circles, not at all where my camera is facing
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.
You'll need to share the latest code if you need further help.
Camera : https://gdl.space/izuqelugow.cs
Movement : https://gdl.space/gucoxemucu.cpp
Sorry to bug you so much, I do really appreciate the help though.
Why are you still multiplying the rotation here?
rb.MoveRotation(rb.rotation * finalRot);
Well, it doesnt do anything if its there or not, I tested it both ways. If thats the case, I just figured theres no reason to worry about it.
Or rather, if there is, I don't see anything different if its there versus if there isn't
There is a reason to worry if it's plainly incorrect code.
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.
What docs exactly do that?
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.
One: How would I do that?
Two: I'm dumb as a box of crayons and not the most logical so that tracks
Just use the final rotation in MoveRotation. No need to multiply it by anything
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
Share the updated code. As well as your hierarchy/inspector setup.its likely that the issue is more than just code related.
Also, a video of the issue would be helpful
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?
If the video shows the hierarchy and the relevant objects inspectors it will do
Camera : https://gdl.space/aqetexecox.cs
Movement : https://gdl.space/lexubihoxo.cpp
Also, just as a heads up, I did not move diagonally by using two keys at all in the clip. Just holding w, I would walk forward normally, then holding w would have me walk diagonally, then by the end I was walking in circles by holding w.
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!
I guess that means that the issue is fixed..?