- Home /
Question by
barnasaradavid · Jul 15, 2019 at 03:47 PM ·
cameracamera-movementcamera-lookcamera followcamera rotation
Returning the camera back to original position,
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;
}
},
Comment
Answer by pyramidhead01 · Jul 15, 2019 at 03:57 PM
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;
Let me know if that worked if you need more help
I didn't succeed, but i have a new idea for the character and the camera movement.