- Home /
Orbit camera for VR Cardboard
I'm trying to make a 3rd person camera controller for using Google VR sdk, but i face a problem when moving camera to keep it looking at Player object
My scene structure like this:
--- Player
--- CameraContainer
------ MainCamera
How can i calculate the position of CameraContainer base on Rotation of MainCamera to keep Player object always between the center of camera
The problem same with bellow question but i need move CameraContainer base on rotation of MainCamera
https://forum.unity.com/threads/rotate-the-camera-around-the-object.47353/
====================================================================
Update:
I'm using this script to move camera positon but it works with Mouse Input, any idea to convert it using MainCamera rotation?
public class CameraFollow : MonoBehaviour {
public Transform target;
float distance = 10f;
int cameraSpeed = 5;
float xSpeed = 175f;
float ySpeed = 75f;
int yMinLimit = 20;
int yMaxLimit = 80;
int minDistance = 5;
int maxDistance = 20;
float x = 0f;
float y = 0f;
// Use this for initialization
void Start () {
var angles = transform.eulerAngles;
x = angles.y;
y = angles.x;
// Make the rigid body not change rotation
var rigidbody = GetComponent<Rigidbody>();
if (rigidbody)
rigidbody.freezeRotation = true;
}
// Update is called once per frame
void LateUpdate () {
//Zooming with mouse
distance += Input.GetAxis("Mouse ScrollWheel") * distance;
distance = Mathf.Clamp(distance, minDistance, maxDistance);
//Detect mouse drag;
if (Input.GetMouseButton(0))
{
x += Input.GetAxis("Mouse X") * xSpeed * 0.02f;
y -= Input.GetAxis("Mouse Y") * ySpeed * 0.02f;
}
y = ClampAngle(y, yMinLimit, yMaxLimit);
var rotation = Quaternion.Euler(y, x, 0);
var position = rotation * new Vector3(0.0f, 0.0f, -distance) + target.position;
transform.position = Vector3.Lerp(transform.position, position, cameraSpeed * Time.deltaTime);
transform.rotation = rotation;
}
float ClampAngle(float angle, float min, float max)
{
if (angle < -360)
angle += 360;
if (angle > 360)
angle -= 360;
return Mathf.Clamp(angle, min, max);
}
}
Let an empty gameobject follow the player, matching his position. Attach the camera to it. Rotate the empty based on cardboard orientation.
@hexagonius Thanks for reply! How can i calculate the position of empty gameobject based on VR camera rotation? Because when VR camera rotated, i have to change the position of empty gameobject localy around Player
Why? As I said, make the camera dependent on the empty, not the other way around.