Camera follow Player Car

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:

void Start ()
    {
        offSet = target.position - transform.position;
    }

    void LateUpdate ()
    {
        Vector3 cameraPosition = target.position - offSet;
        cameraPosition.x = 0f;
//        cameraPosition.z = -10f;

        transform.position = cameraPosition;
//        transform.position = Vector3.Lerp (transform.position, cameraPosition, Time.deltaTime * smoothness);
    }

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.

One more points, this is 2D game…

Please give me some suggestion for this.

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.

@LiterallyJeff thanks for your quick reply.
Yes my player car moving with physics velocity.

So as per your saying, I have to use this statement like this,

transform.position = Vector3.Lerp (transform.position, cameraPosition, Time.deltaTime);

Then its moving really slowly an object so for achieving desire speed, I multiply deltaTime with suitable values each time.

Even I don’t know, how Vector3.MoveTowards will work.

@JoshuaMcKenzie can we use Android and iOS device in Unity Profiler data display?

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

https://docs.unity3d.com/ScriptReference/Vector3.SmoothDamp.html

Now some sort of time spent with same, I have following code but its still require improvements.

float smoothTarget = Mathf.SmoothDamp (transform.position.y, target.position.y - offSet.y, ref velocity, smoothness);

        Vector3 cameraPosition = transform.position;
        cameraPosition.y = smoothTarget;
        transform.position = cameraPosition;

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.

Video Link
https://drive.google.com/open?id=0B6CFlS0aZDPiNjJuakI2WUthM1k

Please give some suggestion to move ahead.

If your car is using Rigidbody, then you will have to put your camera movement code into FixedUpdate instead of LateUpdate.

If you are indeed using Rigidbody, set it to Interpolate if you haven’t already.

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?

1 Like

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
  • Double tap flying car functionality

2857155–208953–Player.cs (7.47 KB)

Please give me some reply…

In FixedUpdate you’re using Time.deltaTime. Try using Time.fixedDeltaTime instead.

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.

Try make Edit > Project Settings > Time > Fixed Timestep to a smaller number and see what happens.