Camera Vector

How can i replace new Vector3(0, 0, -10); with new Vector3(0, 0, mainCameraSize); ?

In this lecture, you were discussing about “Size” property of the camera. How can I use this property in my vector 3?

I was trying to debug camera object, but looks like im doing it wrong

        var output = JsonUtility.ToJson(UnityEngine.Camera.main, true);
        Debug.Log(output);

Hi @Space7Panda,

Welcome to our community! :slight_smile:

What is the problem you would like to solve? And why would you want to use the size of the camera?

Hello @Nina
I want to use the “Size” parameter from the Main camera instead of writing hardcode in camera script.

using UnityEngine;

public class FollowCamera : MonoBehaviour
{
    [SerializeField] GameObject thingToFollow;
    void LateUpdate()
    {
        transform.position = thingToFollow.transform.position + new Vector3(0, 0, -10);        
    }
}

What exactly do you want to do? Did you randomly pick the size or is there a reason why you want to use it?

The size refers to the height of the camera. It is defined as height / 2 in WU (World Units). I do not see where you would want to use that in this context. If you could explain your idea a bit further, I’m sure we’ll find a solution. :slight_smile:

transform.position = thingToFollow.transform.position + new Vector3(0, 0, -10);

Allows me to create following camera in a script.

-10 is distance between gameobject and the camera

I could create variable with [SerializeField], to remove hardcode, but i want to pull value from the Main Camera game object instead of creating variable that contains same entity value
photo_2021-10-02_16-20-06

The camera size does not have anything to do with the position or distance between objects. It refers to the viewport. If you want to use the position of the camera, use the transform.position of the camera.

float distance = Camera.main.transform.position.z - transform.position.z;
1 Like

Oh, thank you.
Also, I wonder how I can debug objects in Unity, for example, I want to see what is stored in Camera.main.transform.position object. Can I convert it to string or JSON somehow? Or is there a more efficient way?

With Debug.Log, you could log the value of Camera.main.transform.position into your console.

But i want to debug whole object, with all key => value pairs without checking source code of object interfaces

Like this one in C#

Newtonsoft.Json.JsonConvert.SerializeObject(new {foo = "bar"})

Found it, i just need to use the following:

using UnityEditor;

Debug.Log(EditorJsonUtility.ToJson(Camera.main.transform));

Good job. :slight_smile:

Is the problem solved?


See also:

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms