Camera is not showing anything when following player

so i have a 2D game and there is a camera, and the player moves, the camera follows it with a script. so if the script is off, it shows all the terrain, but if i turn on the script, it only shows the colour background, and none of the terrain and not the player.

this is the script:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class offsetFollow : MonoBehaviour
{

    public Transform followTransform;
    public Vector2 offset;


    // Update is called once per frame
    void FixedUpdate()
    {
        transform.position = new Vector2(followTransform.position.x + offset.x, followTransform.position.y + offset.y);


    }
}

image of the camera settings (i just started making the game today, its a prototype)

and this is what the camera gives me in the game:

i checked the position of the camera in-game and it is following the player.

so what is the problem?

my OS: Windows 10
Unity version: 2020.3.13f1
my game is for Android

1 Answer

1

You didn’t add a z offset in your code, the camera will be at the same Z location as your player.
You need to add a Z offset. Try this:

transform.position = new Vector3(followTransform.position.x + offset.x, followTransform.position.y + offset.y, followTransform.position.z - offset.z);

you will need to change your offset vector2 to vector3

Thanks a lot!

where do i put this? this is my code using System.Collections; using System.Collections.Generic; using UnityEngine; public class CameraFollow : MonoBehaviour { public Transform target; // The object to follow public float smoothing = 5f; // The smoothing of the camera movement void FixedUpdate() { // Calculate the position the camera should be in Vector3 targetCamPos = target.position; // Smoothly move the camera towards that position transform.position = Vector3.Lerp(transform.position, targetCamPos, smoothing * Time.deltaTime); } }