Unity is sometimes a little weird and/or tricky.
First of all, one of the problems you’re facing can be solved completely (Jitter) but maybe both, but that depends on which delay you’re takling about (I assume the first one).
- The one that can be seen when you (only) move the object and the line lags behind, i.e. is not exactly at the mouse’s position? Can be solved. There’s a mistake in the logical order.
- The delay which you notice when you move the mouse too quickly?
As far as I know, the system input cannot be tracked completely in “real time” with the current Input system, as it only updates/samples input once per-frame and does not directly poll input in “real time” whenever you ask for it. All the mouse movement which occurs during the frame (until the next sample is taken) is just not tracked and it appears that you’ve got a sligth delay when moving your mouse to fast. Not sure if prediction techniques could help here. But I wouldn’t consider this annoying.
*Edit Check @takatoks answer below, that would eliminate this kind of delay from these mid-update-changes
Anyways, what’s causing that one specific part of the delay, and why does it cause what you describe as jitter?
In short: You’re missing an essential detail in the execution order of your game logic.
Skip to the listing of execution order in the end if that’s too much to read.
But first, let’s see why that effect, which you call jitter, appears to be more obvious, especially with higher FPS.
=> instable framerates.
Just for example, select a moving object so that you can observe it in the inspector, the FPS in the stats window will usually increase (on all of my machines at least) but is usually more instable (you can verify that with logging).
Contrary, If you select nothing so that the inspector is not constantly redrawn, FPS appear to be more stable and also (usually) lower. The stable framerate reduces the visual jittering effect, naturally.
When you’ve got a high framerate, a peak in the frame update time can sometimes be more noticeable and that’s what’s causing the visual jittering effect, which (again) only occurs due to the actual mistake which let’s your line appear to lag behind.
Now assume you’ve got completely stable FPS rate. You wouldn’t notice the jittering anymore, but there’s still a problem which contributes to your input lagging behind and, in the end, will be the reason the jitter effect can actually occur.
The camera plays an important role in your setup and your game logic, it’s something you may not think about unless you face effects as those described in your post.
You’re executing your game logic in the following order.
- You retreive mouse movement input in Update and move the object in update. That’s correct.
- From now on you can retreive the start position for your LineRenderer. (You’re actually doing that in 4, that’s totally okay as the object has finished moving).So that’s correct.
- You retreive the mouse position (still) in Update and transform it into world space coordinates in order to get the LineRenderers end-position. Is that correct? Wait…
- You set the LineRenderers positions. Well, 2) was correct, but 3 might be wrong… So this might be wrong as well
- You set Camera’s position (in LateUpdate), as it depends on the tracked final position of the tracked object Nothing wrong with it.
It starts to get messed up in step 3.
Think about it… You’re using the camera to transform the mouse’s screen position into world space coordinates. But the camera has still to be moved later on… For the rendering part, you’ve actually polled the end position too early.
It’ll be a position that was calculated using the camera’s old position. So there’ll be tiny offset which might be your “delay” unless you’re talking about the other part of it, which I mentioned above.
Now finally, I hope you’re still encouraged to try this order:
- move the object with the mouse axis input, when it’s done, your LineRenderer’s start position is ready to be used
- move the camera here (LateUpdate or ensure the Update is called after the LineControllers Update) - I’d say keep LateUpdate
- now retreive the end position using the camera’s new position
- set the LineRenderers positions
The line should always be on point, except for the mouse position changes which occur during 2 input samples.