Hey guys
I am new to unity it is my first project, I am doing a simple player movement with arrow keys, and I want to make a button when pressed it resets the player to a certain position 0,0,0 . or in my case I want to save a certain position and then reset to it. I have attached the camera to the player and made move camera script and the player moves just fine but when I press the button to reset the position the transform.position do not make any changes in the game. my code is as the following
public GameObject mainCamera;
public GameObject DeactivateEgoButton;
public GameObject LoadCameraPositionButton;
public GameObject SaveCameraPositionButton;
public GameObject Player;
private Vector3 savedPlayerPosition;
private Quaternion savedPlayerRotation;
private Quaternion savedCameraRotation;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
}
public void activateCamera()
{
Camera.main.gameObject.SetActive(false);
mainCamera.SetActive(true);
DeactivateEgoButton.SetActive(false);
SaveCameraPositionButton.SetActive(true);
Debug.Log("Camera Activated");
}
public void saveCameraPosition()
{
// we have to save position of the player that is why we use .parent
savedPlayerPosition = mainCamera.transform.parent.position;
savedPlayerRotation = mainCamera.transform.parent.rotation;
savedCameraRotation = mainCamera.transform.rotation;
LoadCameraPositionButton.SetActive(true);
Debug.Log(savedPlayerPosition);
mainCamera.transform.parent.position = new Vector3(0, 0, 0);
}
public void loadCameraPosition()
{
Debug.Log(mainCamera.transform.parent.name);
Debug.Log(mainCamera.transform.parent.position);
mainCamera.transform.parent.position = savedPlayerPosition;
mainCamera.transform.parent.rotation = savedPlayerRotation;
this.mainCamera.transform.rotation = savedCameraRotation;
mainCamera.transform.parent.position = new Vector3(0,0,0);
Debug.Log(mainCamera.transform.parent.position);
//if(!mainCamera)
//{
// Camera.main.gameObject.SetActive(false);
// mainCamera.SetActive(true);
// DeactivateEgoButton.SetActive(false);
// SaveCameraPositionButton.SetActive(true);
//}
}
within the picture the buttons for load camera position when pressed is supposed to call loadCameraPosition to reset camera position, but it does not work
Thanks in advance