I want my camera to go back to the original position. It’s attached to the player. Any ideas?
Here is the full script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CameraController : MonoBehaviour
{
public GameObject target;
public float xSpeed = 3.5f;
float sensitivity = 17f;
float minFov = 35;
float maxFov = 100;
// Update is called once per frame
void Update()
{
if (Input.GetMouseButton(1))
{
transform.RotateAround(target.transform.position, transform.up, Input.GetAxis("Mouse X") * xSpeed);
transform.RotateAround(target.transform.position, transform.right, -Input.GetAxis("Mouse Y") * xSpeed);
}
//ZOOM
float fov = Camera.main.fieldOfView;
fov += Input.GetAxis("Mouse ScrollWheel") * -sensitivity;
fov = Mathf.Clamp(fov, minFov, maxFov);
Camera.main.fieldOfView = fov;
}
},
Add a Vector3 position and the you can reference that whenever you want, I’ll give you an example:
public GameObject target;
public float xSpeed = 3.5f;
float sensitivity = 17f;
float minFov = 35;
float maxFov = 100;
Vector3 originalPosition;
//Example to get position, just type this line of code in for whatever position you are trying to move back to
void Start
{
originalPosition = camera.main.transform.position;
}
// Update is called once per frame
void Update()
{
if (Input.GetMouseButton(1))
{
transform.RotateAround(target.transform.position, transform.up, Input.GetAxis("Mouse X") * xSpeed);
transform.RotateAround(target.transform.position, transform.right, -Input.GetAxis("Mouse Y") * xSpeed);
}
//ZOOM
float fov = Camera.main.fieldOfView;
fov += Input.GetAxis("Mouse ScrollWheel") * -sensitivity;
fov = Mathf.Clamp(fov, minFov, maxFov);
Camera.main.fieldOfView = fov;
}
Then whenever you want to snap back, you type this line of code in:
camera.main.position = orinalPosition;