Nirmal Purohit
Nirmal Purohit

Reputation: 67

How to make camera follow a car?

I'm developing car racing game in unity. I put car moving script in bot car and main camera. Camera is following car when moving forward but when car collides with other object and car diverts its position or when I turn car turns also camera but earlier than car so car goes out of scope. I tried to put character controller, tried putting camera in hierarchy still it's not following car smoothly even with smooth follow script. So tell me either way to make camera follow car as we see in other car racing game.

Upvotes: 2

Views: 11100

Answers (5)

bnzzy
bnzzy

Reputation: 1

One way to do it would be to just set the camera as a child of the car. This will essentially lock the camera and give it the same offsets in position and rotation between the car and the camera. You could also try Cinemachine, which is a package that has a lot of useful features that will basically do the same thing while also giving you some more features that you may want. There's quite a bit of documentation on it if you really want to get into it:

https://unity.com/unity/features/editor/art-and-design/cinemachine

Upvotes: 0

user16223274
user16223274

Reputation:

You can

Set the camera as a children of your car (the object you want to follow)

Write this code taken from here

using UnityEngine;
using System.Collections;

public class CameraController : MonoBehaviour {

    public GameObject player;        //Public variable to store a reference to the player game object


    private Vector3 offset;            //Private variable to store the offset distance between the player and camera

    // Use this for initialization
    void Start () 
    {
        //Calculate and store the offset value by getting the distance between the player's position and camera's position.
        offset = transform.position - player.transform.position;
    }

    // LateUpdate is called after Update each frame
    void LateUpdate () 
    {
        // Set the position of the camera's transform to be the same as the player's, but offset by the calculated offset distance.
        transform.position = player.transform.position + offset;
    }
}

Upvotes: 0

zain ul din
zain ul din

Reputation: 629

The setting game camera is really challenging especially when it's come to vehicle camera control because camera movement speed depends upon vehicle speed and it's a little bit difficult to control the camera for a vehicle using the C# script.

I highly recommend you should use unity build in ' Cinemachine Package ' you can install it for free. if you want to learn about cinemachine and go ahead and check out this video https://www.youtube.com/watchv=X33t13gOBFw&list=PLOBPWyvPXShNN01VQ1LLPqfCtqeRo95Z1&index=23 and by the way, in this video they are setting cinemachine camera for vehicle

Upvotes: 0

Ethan K-B
Ethan K-B

Reputation: 135

Just add the camera to the car as a child object

Upvotes: -1

user2983378
user2983378

Reputation:

What I recommend for this type of game(where the camera is set to follow a certain object, from place A to place B) is to parent an empty gameobject where you want the camera to end up to the car and another empty gameobject parented to the car for where you want the camera to be looking at.

After this, use the basic camera follow and camera lookat scripts that come with the standar assets and play around with the smoothing factors to suit your game. You have to make sure you imported the scripts package for your project.

OR, if the package is imported, select your camera, go to the component menu, and under "Camera Control" select the "Smooth Follow" script.

If you need the camera to dynamically change position depending on (for example) where the car is on the road, I would consider the same solution but using iTween to alter both gameobjects positions along a predefined path.

More about: smoothfollow: http://wiki.unity3d.com/index.php/SmoothFollow2 lookat: http://docs.unity3d.com/ScriptReference/Transform.LookAt.html

Upvotes: 1

Related Questions