Reputation: 11
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;
[SerializeField] private Vector3 initialCameraPosition = new Vector3(3f, 1f, 2.5f);
[SerializeField] private Vector3 initialCameraRotation = new Vector3(30f, 45f, 0f);
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 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);
}
}
Upvotes: 0
Views: 36
Reputation: 90813
I'd claim because the initialCameraPosition
is conflicting with the initialCameraRotation
.
The initial position is completely overwritten by the latter and the rest of the code is purely driven by the distance and orientation.
In fact the initial position almost doesn't matter at all - except for calculating the distanceFromTarget
.
In this kind of orbit camera setup you can not control both initial position and pitch, yaw
at the same time. You have to decide for one of these and currently the initialCameraRotation
is the one that wins.
Upvotes: 1