I want my camera follow to my Player Car. But its not showing smoothness in actual device. There is no problem of FPS because its really small game. So definitely its camera movement related issue.
In Unity editor its moving nice but on actual device movement was not that much good so that I can say smooth. Here is my simple code that I was using:
There are many camera following related code exist but those are really complex so I don’t prefer those. I want really simple that understood by every one clearly.
For one thing, that is not how you use Lerp. The final parameter of Lerp is a number between 0 and 1 that represents a percentage of how much to interpolate between the first two parameters. ie 0.5 would be halfway. Using a t parameter of Time.deltaTime will move the position by a percentage each frame but the speed will fluctuate with framerate and it will never actually reach the second parameter.
You probably want to be using Vector3.MoveTowards instead, with the same parameters you have now.
Whether or not that would cause your smoothing issue, I’m not sure. If you’ve tested this commented version, then obviously it isn’t the problem.
Does your “target” move using physics or during FixedUpdate?
While the way lerp is used here isn’t used to its original intention, its fine because it creats a “rubber-banding” motion and the camera doesn’t need to reach the target cameraposition exactly.
I’m positive that the issue is not with this code (however you didn’t post all the code). we can speculate all day what the cause is. but the truth is this is not enough information.
simplicity of a project doesn’t really mean much if the scenes aren’t built properly. I can make an empty scene and add two Unity objects in the wrong place that will make any mobile device chug. you should really use the profiler while attached to the device and see what info it spits out. my metric is to try to keep total ms time under 10 (16 is the hard cap for me) but more often than not (especially for mobiles) its the GC allocation that’s the real killer.
The rubber-banding motion is a trick of the implementation, and not good practice. Reaching the destination is needed if you ever wanted to check if the camera has reached the target, in this scenario the Camera will never stop moving. Its speed is also dependant on framerate which is not desirable.
You can achieve the same results while actually reaching the destination and moving framerate independently by using Mathf.SmoothStep on the t parameter of a proper lerp, or using Vector3.SmoothDamp on the whole position.
You could try this:
private Vector3 vel; // put this variable at the class-level
transform.position = Vector3.SmoothDamp(transform.position, cameraPosition, ref vel, smoothing);
// smoothing is the approximate amount of time it will take to reach the destination
// you can also specify a max-speed after the smoothing parameter
Still there is noticeable jerks in movement.
Tried with different frame rate, different update methods as well but mostly performance remain same.
There is no issue of frame rate as well.
At present I was using Unity 5.3.5 version.
In actual android device its clearly noticeable.
Yes, player car has rigidbody component and player car is moving via up velocity.
If I use fixed update in camera follow then its showing so much vibration of player car.
I already applied Interpolation in rigidbody component.
Physics-based movement should use FixedUpdate and Time.fixedDeltaTime.
Render-Based movement should use LateUpdate and either Time.deltaTime or Time.smoothDeltaTime.
pretty much everything else can just use Update and Time.deltaTime.
the camera movement seems smooth to me, I would look into the vehicle’s movement
whats the code for the car’s movement?
All you said, I completely understand.
At present player car moving using velocity so code exist in FixedUpdate but if I put camera follow code in FixedUpdate then movement become so much vibrated.
As per you asked for player car movement code, I have attached script file that control car movement.
Following script doing following thing so that code also included:
Player car movement towards upside
Left and right swipe detection to change lan of track with little rotation of car
Basically this thing not working with me, its create still more poor behaviour.
deltaTime I was using just in left and right turn of car. Straight movement running with velocity.
In straight movement also looking jerking though I don’t know its camera or player car movement.