0
\$\begingroup\$

I am making a 2D side-scroller racing game. The Player is a car that moves on a 2D terrain already created. The Camera follows the Player. I also wanted to created more depth by making the background follow the y-axis of player so that the background doesn't look like a plain picture. The effect I want to replicate is that when the player jumps the camera just zooms out.

I am using a perspective camera so it zooms out by moving it slightly farther away from the Player. Unfortunately, the effect isn't smooth.

The platform is using 2D edge collider. The movement gave me motion sickness and I just don't know what I should be doing to make this follow nicely.

I have tried using smooth damp that gave me spring effect which isn't needed here. I used Lerp which just doesn't work at all. It's dang slow and when I make it fast it just same undesirable behaviour. The MoveTowards works a bit but not quite really. The smoothness is just not what I want.

For reference, my game is pretty much same like the I hate zombies. check video https://youtu.be/YQZoBC1hVyk?t=31s

attached is my code.

 Vector3 newPos;
public Vector3 camOffSet = Vector2.zero;
public float speed = 2;
Vector3 velocity = Vector3.zero;
void FixedUpdate()
{
    if (CarController.Instance != null)
    {
        newPos = CarController.Instance.transform.position + camOffSet;
        if (CarController.Instance.IsGrounded)
        {

            newPos.z = -10;
        }
        else
        {
            float zz = Mathf.Clamp(-10 - newPos.y, -20, -10);
            newPos.z = Mathf.Clamp(-10 - newPos.y, -20, -10);
            //newPos.x = CarController.Instance.transform.position.x + (camOffSet.x * 3);
            newPos.x += (zz * -0.4f);
            //newPos.z = Mathf.MoveTowards(transform.position.z, Mathf.Clamp(-10 - newPos.y, -20, -10),
            //    speed * Time.deltaTime);
        }
        //transform.position = newPos;
        transform.position = Vector3.MoveTowards(transform.position, newPos, speed * Time.deltaTime);
    }
}
\$\endgroup\$
7
  • 1
    \$\begingroup\$ 2 notes: this is a pretty common thing to code, so you should probably do a little more searching. There are plenty of examples of a parallax camera. Secondly, post your code. Not being smooth could be because you never wrote anything, or what your wrote isn't working. Exactly which and why won't be apparent to anyone attempting to answer the question withoutthe code of your attempt. \$\endgroup\$ – Stephan Jan 2 '18 at 13:58
  • \$\begingroup\$ ok Stephan I'll edit my question. \$\endgroup\$ – Adnan Nazir Jan 2 '18 at 15:02
  • 1
    \$\begingroup\$ You still need to post a code sample for us to be able to help you. \$\endgroup\$ – Stephan Jan 2 '18 at 16:24
  • 1
    \$\begingroup\$ The cam behavior in the zombie game is more complicated than it seems i think. Looks like the cam is positioned so that the car is at bottom of the screen when closer to the ground. At 33 seconds the car takes off and flies over a peak. As the distance to the ground changes the camera moves to position car at bottom of the screen and then moves away again as the peak passes. Also looks like camera distance uses the car distance from the floor. At 36 seconds the car goes over two humps and the camera moves in and out the closer the car is to the floor. Still makes me feel a bit sick :) \$\endgroup\$ – RobM Jan 3 '18 at 11:55
  • \$\begingroup\$ Agreed ! can you have a look at this one as well ? @RobM? youtube.com/watch?v=xO_mEXo6mNA Can you suggest how I can force the background to be slow ? It's just environment my player and camera. I am moving camera. This is perspective camera and I am just making it follow the Player. \$\endgroup\$ – Adnan Nazir Jan 3 '18 at 12:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.