Hi,
I’m starting my scene by a flythrought of the cam1 then the cam1 stop and switch to cam2 which should be a the exactly same position that cam1 at the end of the flythrought. So basically having a camera switching seemless.
The cam1 is linked to a cube animated with a path constraint in max. I know the exact end position by using pause at the end and take it off the cube (not child anymore).
The cam2 is using this script:
using UnityEngine;
using System.Collections;
public class RotateCam : MonoBehaviour {
//-------------Rotation Variables---------------------
//Rotation Speed
public float xSpeed = 200.0f;
public float ySpeed = 200.0f;
public float SpeedCoef = 0.02f;
// Y Axis Limits
public float yMinLimit = -80.0f;
public float yMaxLimit = 80.0f;
//Damping
public float RotDamp = 5.0f;
//AmtToRotate in degrees
private float xDeg = 0.0f;
private float yDeg = 0.0f;
//Rotation from current to desired
private Quaternion currentRotation;
private Quaternion desiredRotation;
private Quaternion rotation;
//---------Position Variables--------------------------
// Target
public Transform target;
public Vector3 targetOffset;
// Distances and positions
public float distance = 5.0f;
public float maxDistance = 20.0f;
public float minDistance = 0.6f;
private float currentDistance;
private float desiredDistance;
private Vector3 position;
//Zoom parameters
public int zoomRate = 150;
public float zoomDamp = 5.0f;
//interrupteur GUI
public static bool interrupteur = true;
// Use this for initialization
void Start () {
//-------------Set initial Rotation--------------------
rotation = transform.rotation;
currentRotation = transform.rotation;
desiredRotation = transform.rotation;
xDeg = Vector3.Angle(Vector3.right, transform.right );
yDeg = Vector3.Angle(Vector3.up, transform.up );
//-----------Set initial Position----------------------
distance = Vector3.Distance (transform.position, target.position);
currentDistance = distance;
desiredDistance = distance;
position = transform.position;
}
// Update is called once per frame
void Update()
{
if (interrupteur == true)
{
//--------------Rotation--------------------
//Check if the mouse button is on
if (Input.GetMouseButton(0))
{
//Set the AmtToRotate
xDeg += Input.GetAxis("Mouse X") * xSpeed * SpeedCoef;
yDeg -= Input.GetAxis("Mouse Y") * ySpeed * SpeedCoef;
// Make sure that the yDeg is still in the Y limits
yDeg = ClampAngle(yDeg, yMinLimit, yMaxLimit);
//Use the AmtToRotate to rotate the camera
desiredRotation = Quaternion.Euler(yDeg, xDeg, 0);
currentRotation = transform.rotation;
//Add a damping
rotation = Quaternion.Slerp(currentRotation, desiredRotation, Time.deltaTime * RotDamp);
transform.rotation = rotation;
}
//---------------Position------------------
//Set the AmtToMove
desiredDistance -= Input.GetAxis("Mouse ScrollWheel") * Time.deltaTime * zoomRate * Mathf.Abs(desiredDistance);
//Clamp the zoom min/max
desiredDistance = Mathf.Clamp(desiredDistance, minDistance, maxDistance);
//Add the damping
currentDistance = Mathf.Lerp(currentDistance, desiredDistance, Time.deltaTime * zoomDamp);
// New position
position = target.position - (rotation * Vector3.forward * currentDistance);
transform.position = position;
}
}
private static float ClampAngle (float angle, float min, float max)
{
if (angle < -360)
angle += 360;
if (angle > 360)
angle -= 360;
return Mathf.Clamp (angle, min, max);
}
}
With this script, when you hit play, the camera moves to look at the target and be at the right distance.
I’m trying different things since few hours now and nothing works at all.
the important part is this equation:
position = target.position - (rotation * Vector3.forward * currentDistance);
I know the position and the target.position, I can calculate the currentDistance with a distance(cam, taget), I can’t divide vector3.
I don’t know how to fix this, I understand why the camera moves at start but I can’t write the right equation to move the camera to the position that I want…
I know that my cam1 doesn’t have the exact right rotation: the cam2 rotation is always facing the target so the cam1 isn’t facing exactly the target. But I want to fix one step at the time fist the position, because the switch is really obvious. For the rotation i’ll be able to make the cam1 end rotation as close as possible as the cam2 in 3dsmax.
Any ideas ?