Camera initial position in PLAY mode

Hello! I would like help in that I have a prefab camera with these transform.position data:

Scene:Vector3(-2.86999989,1.01999998,-0.810000002).

This row in the code:

[SerializeField] private Vector3 initialCameraPosition = new Vector3(-2.86999989, 1.01999998, -0.810000002);

I wrote a script that allows me to start my camera rotation with these initial transform.position data. However, when I start the script, the prefab (camera) transform.position values ​​are immediately overwritten:

play:Vector3(-2.72023177,0.810079217,-1.23373544).

The difference between Scene and Play transform.position data:

X axis: 0.1498
Y axis: -0.2099
Z axis: -0.4237

The Target on which I drag the script has Vector3(0,0,0,) transform.position.

  • I use only 1 camera.;

  • Other scripts doesn’t use the camera.;

  • There are no other settings affecting on the camera.

My question is why does the initial value of my camera jump/change in PLAY mode?

Thank you in advance for your help.

The script:

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

public class CameraController : MonoBehaviour
{
    [SerializeField] private Transform target; // A célpont, amely körül forog a kamera
    [SerializeField] private Vector3 initialCameraPosition = new Vector3(3f, 1f, 2.5f); // Kezdeti kamera pozíció
    [SerializeField] private Vector3 initialCameraRotation = new Vector3(30f, 45f, 0f);  // Kezdeti kamera rotáció (pitch, yaw, roll)

    public float sensitivity = 100f; // Forgatás érzékenysége
    private float _yaw = 0f;         // Az aktuális forgási szög vízszintes tengelyen
    private float _pitch = 0f;       // Az aktuális forgási szög függőleges tengelyen
    private float distanceFromTarget = 5f; // Távolság a célponttól

    private float minPitch = 5f;     // Minimális függőleges szög
    private float maxPitch = 90f;    // Maximális függőleges szög

    private Transform cameraObject;  // A kamera objektum, amit dinamikusan megkeresünk

    void Start()
    {
        // Kamera objektum megkeresése, ha nincs előzőleg hozzárendelve
        cameraObject = Camera.main.transform;

        if (target == null)
        {
            Debug.LogError("Nincs célpont (Target) hozzárendelve!");
        }

        if (cameraObject == null)
        {
            Debug.LogError("Nincs kamera a jelenetben!");
            return;
        }

        // Beállítjuk a kezdeti pozíciót és rotációt
        cameraObject.position = initialCameraPosition;
        cameraObject.rotation = Quaternion.Euler(initialCameraRotation);

        // Távolság és forgási szögek kiszámítása a megadott kezdőpozícióból
        Vector3 targetDirection = cameraObject.position - target.position;
        distanceFromTarget = targetDirection.magnitude;

        _yaw = initialCameraRotation.y; // A yaw értéke a kezdeti rotációból
        _pitch = initialCameraRotation.x; // A pitch értéke a kezdeti rotációból
    }

    void Update()
    {
        HandleInput();

        // Korlátozzuk a pitch értéket
        _pitch = Mathf.Clamp(_pitch, minPitch, maxPitch);

        // Forgatjuk a kamerát a célpont körül
        RotateCamera();
    }

    private void HandleInput()
    {
        Vector2 inputDelta = Vector2.zero;

        // Mobil érintés
        if (Input.touchCount > 0)
        {
            Touch touch = Input.GetTouch(0);
            inputDelta = touch.deltaPosition;
        }
        // Egér
        else if (Input.GetMouseButton(0))
        {
            inputDelta = new Vector2(Input.GetAxis("Mouse X"), Input.GetAxis("Mouse Y"));
        }

        // Frissítjük a forgási értékeket
        _yaw += inputDelta.x * sensitivity * Time.deltaTime;
        _pitch -= inputDelta.y * sensitivity * Time.deltaTime;
    }

    private void RotateCamera()
    {
        // Kiszámítjuk a forgást a célpont körül
        Quaternion rotation = Quaternion.Euler(_pitch, _yaw, 0f);
        Vector3 positionOffset = rotation * new Vector3(0, 0, -distanceFromTarget);

        // Beállítjuk a kamera pozícióját és rotációját
        cameraObject.position = target.position + positionOffset;
        cameraObject.LookAt(target);
    }
}

Sounds like a bug!

If you wanna debug your own camera code, feel free (see below), but you probably should at least consider instead using Cinemachine from the Unity Package Manager.

There’s even a dedicated Camera / Cinemachine area: see left panel.

If you insist on making your own camera controller, do not fiddle with camera rotation.

The simplest way to do it is to think in terms of two Vector3 points in space:

  1. where the camera is LOCATED
  2. what the camera is LOOKING at
private Vector3 WhereMyCameraIsLocated;
private Vector3 WhatMyCameraIsLookingAt;

void LateUpdate()
{
  cam.transform.position = WhereMyCameraIsLocated;
  cam.transform.LookAt( WhatMyCameraIsLookingAt);
}

Then you just need to update the above two points based on your GameObjects, no need to fiddle with rotations. As long as you move those positions smoothly, the camera will be nice and smooth as well, both positionally and rotationally.

If you want to debug your camera code:

By debugging you can find out exactly what your program is doing so you can fix it.

Use the above techniques to get the information you need in order to reason about what the problem is.

You can also use Debug.Log(...); statements to find out if any of your code is even running. Don’t assume it is.

Once you understand what the problem is, you may begin to reason about a solution to the problem.

Remember with Unity the code is only a tiny fraction of the problem space. Everything asset- and scene- wise must also be set up correctly to match the associated code and its assumptions.

Dear Kurt-Decker! Thanks for the quick reply. I will look at the recommended asset. My Script Editor doesn’t display debug, that’s why I asked on this interface. I want to learn how to write a script well, so I’ll see if I can solve it. Thanks for the advice. :v: :ok_hand:

Moreover, it will be a game and the player must be able to walk around a table to complete his tasks. Because of the existing UI elements, we need the exact camera position to start.