Build Crossy Road Replica Part-3

 



Next step is to make the camera follow our chicken.

To do this we use a simple script, you can find it from this video:


   the code for the script :

using UnityEngine;

public class camfollow : MonoBehaviour
{
    public Transform target;

    public float smoothSpeed = 0.125f;
    public Vector3 offset;

    void FixedUpdate ()
    {
        Vector3 desiredPosition = target.position + offset;
        Vector3 smoothedPosition = Vector3.Lerp(transform.position, desiredPosition, smoothSpeed);
        transform.position = smoothedPosition;

        
    }
}

if you notice you can see that the last line mentioned in the video has been remove . Its because , in our game rotation is not so important.Also since our character movement is very quick , it feels like the camera tries to snap every time your character tries to move .So it feel better without it.

 Also our camera view is totally different from the game displayed in the video .So for our game what you have to do is, Don't change position of our camera according to the video. instead , just add the script to our camera.(our camera's position and rotation will be such that it will be able to view our character. as we have done in the previous post). Now in the script component you will see there are some parameter which were mentioned in the video 

First , target is our character , so drag our character to that field

Next is smooth speed , you can use trial and error method to find a suitable value for that. For me it was 0.05.

Next is offset , this is the distance your camera should keep from the target. which is the difference between current position of our camera and game character. If your character is near to origin , this value is same as the position of your camera, so copy each value of position to the respective axis field in the script component.

Now my script component values look like :


Now you can click the play button to play the game and you will be able to see the camera following your game character.




---------------------------------------------------------------------------------------------------------------------------

    

No comments:

Powered by Blogger.