- Home /
The question is answered, right answer was accepted
Weird transform reset
So like, i've hit the wall with this...
Trying to implement camera rotation: - Left mouse click and drag - moves character in that direction - All good - Right click rotates camera without player and upon release of right click returns camera to original position - brainfart here...
if (cameraMain)
{
if (Input.GetMouseButton(0))
{
var Y = Input.GetAxis(rotateCameraYInput);
var X = Input.GetAxis(rotateCameraXInput);
tpCamera.RotateCamera(X, Y);
cc.SetCurrentTransform(tpCamera.transform);
}
else if (Input.GetMouseButton(1))
{
if (!originalPos)
{
i++;
originalPos = true;
originalCameraPos = tpCamera.transform;
Debug.Log($"Recorded original X pos: {originalCameraPos.position.x} - debug counter: {i} - Original pos state: {originalPos}");
}
var Y = Input.GetAxis(rotateCameraYInput);
var X = Input.GetAxis(rotateCameraXInput);
tpCamera.RotateCamera(X, Y);
}
else if (Input.GetMouseButtonUp(1))
{
if (originalPos)
{
Debug.Log($"Restoring original X pos: {originalCameraPos.position.x} - Original pos state: {originalPos}");
StartCoroutine(RestoreCameraToOriginalPosition(originalCameraPos));
}
}
}
Both debug messages are triggered and are working.
Problem starts when camera has to return to original position, for some unknown reason to me, when i release right click, originalCameraPos transform is set with current camera transform data. I'm pulling my hair out what is actually resetting this value, since its my implementation and only place where is set once is when you do right click. Added also debug counters, and this is called only once and never more due to boolean check originalPos.
Both originalPos and originalCameraPos are on top of the class, so they are initialized only once when class starts.
I've wrote equal code in C# WinForms where this code works just fine. Original value is once set and never called until right mouse click is released. It has proper recorded value of initial mouse position.
This code is happening in Update() method.
Idk, anyone has any idea ? Am I missing something ? I'm kind of stoned what to do anymore
Thanks in advance.
Answer by nikolaju30 · Dec 18, 2021 at 02:31 PM
So trick was to save position and rotation in 2 different variables instead of complete transform.
Follow this Question
Related Questions
Find amount of vertical movement of object 0 Answers
Door Opening 1 Answer
Will not rotate when going different speed? 1 Answer
setting transform physics2d issue 0 Answers