I’d like to cache/log/create a variable from the cameras Transfrom (location/rotation/scale) on Awake or Start.
Then the user can move around the scene all they want but if the user hits the key C, the camera returns to its position at start/awake.
I’ve tried various things revolving around this
using UnityEngine;
using System.Collections;
public class CameraReset : MonoBehaviour {
private Transform cameraOrigin;
// Use this for initialization
void Start () {
cameraOrigin = gameObject.transform;
}
// Update is called once per frame
void Update () {
if (Input.GetKeyDown(KeyCode.C)) //move camera from wherever it is in the scene back to original position
{
gameObject.transform = cameraOrigin;
}
}
}
Even if the compiler errors were fixed, you have a conceptual problem here. The transform is stored by reference. That is, ‘cameraOrigin’ will refer to the current transform, not a snapshot of the position at the start. Vector3 variables are stored by value, so you want to do something like this:
using UnityEngine;
using System.Collections;
public class CameraReset : MonoBehaviour {
private Vector3 cameraOrigin;
void Start () {
cameraOrigin = transform.position;
}
void Update () {
if (Input.GetKeyDown(KeyCode.C)) //move camera from wherever it is in the scene back to original position
{
transform.position = cameraOrigin;
}
}
}
You can’t assign to the transform of a game object. It is read only. What you can do is save the position, rotation and scaling of the transform and then restore those.
Give it a try. If you don’t come up with it I will post some code.
try this(should point ya in the right direction.):
This script has to be put on main camera to work this way.
using UnityEngine;
using System;
using System.Collections;
public class CameraReset : MonoBehaviour {
private Vector3 cameraOrigin;
private GameObject thisCamera;
private Vector3 cameraRotation;
// Use this for initialization
void Start () {
thisCamera = this.gameObject;
cameraOrigin = thisCamera.transform.position;
cameraRotation = thisCamera.transform.eulerAngles;
}
// Update is called once per frame
void Update () {
if (Input.GetKeyDown(KeyCode.C)) //move camera from wherever it is in the scene back to original position
{
thisCamera.transform.position = cameraOrigin;
thisCamera.transform.eulerAngles = cameraRotation;
}
}
}