Camera stops rendering objects whenever this smooth follow 2D Script is applied

Hi guys, im rather new to unity so maybe there is something to the camera behaviour that I just dont understand yet. However, I have written this smoothFollow2D script in C# to make my Camera follow a square player object when its moving:

    public Transform target;
    public float smoothTime = 0.3f;
    private Transform thisTransform;
    private Vector2 velocity;

	// Use this for initialization
	void Start () 
    {
        thisTransform = GetComponent<Transform>();
	}
	
	// Update is called once per frame
	void Update () 
    {
        Vector2 smoothMove = new Vector2(Mathf.SmoothDamp(thisTransform.position.x,
        target.position.x, ref velocity.x, smoothTime),
        Mathf.SmoothDamp(thisTransform.position.y, target.position.y, ref
        velocity.y, smoothTime));
        thisTransform.position = smoothMove;

	}

This is working very well so far, but when i apply this script to my camera and start the game it simply stops rendering any objects but the background(Skybox). I can see the camera following the player object in the scene view but i see nothing in the game view. I also had this problem before without any sripts and just with 3 objects in my scene, but I cant tell what I did to solve it.

EDIT:

I figured out that the problem has something to do with the initialisation of the targeting Transform, if I leave it empty or just clear the start section there is no rendering issue.

Why would you create a variable just for your transform component? Just use "transform" instead.

I thought it just works that way because you have to acces transform by GetComponent(); So you say I could use it like this.transform?

Storing transform in a reference variable saves processing power. Your script is correct. By assigning vector2 to your camera's position. You set position.z = 0. Does this cause problem?

That was my tought too. Thanks, and yes my problem was caused by the z positioning. I've already found that out by myself, but thanks anyway.

2 Answers

2

Most likely because you set the z position of your camera implicitly to “0” with this script. Now it depends on what type of camera you are using and on it’s parameters.

  • If you use a Perspective camera you have no choice. You have to place the camera at some negative z value in order to actually see something. Otherwise your 2d stuff (which usually is at z == 0) will be behind the cameras near clipping plane.
  • If you use an orthographic camera the z position doesn’t really matter, but you have to use a negative near clipping plane to actually “see” things “behind” the camera position.

Note: A perspective camera can’t have a negative near clipping plane it always has to be greater than 0.

An orthographic camera could have the “near” clipping plane at -inf and the far clipping plane at inf which would make the z position completely obsolete.

I figured it out, it has to be a Vector3 with a fixed value for z Axix, otherwise it will end up in placing the camera behind objects (at z = 0) since they are at z = -10 somehow:

Vector3 smoothMove = new Vector3(Mathf.SmoothDamp(thisTransform.position.x,
target.position.x, ref velocity.x, smoothTime),Mathf.SmoothDamp(
thisTransform.position.y, target.position.y, ref velocity.y, smoothTime),-10f);