Reputation: 261
I have a camera that is orbited, panned around an object to showcase the object in question, because the object is rather large it's easy to potentially lose your position and want to get back to the starting position, I'd like to do this via a button.
I'd also like this transition to not be jarring, a smooth transition between it's current position and the one specified. I realise this is an interp related question and I've no idea where to start with this.
My basic transform code so far is
public Camera MainCamera;
public void UserClickedCameraResetButton()
{
MainCamera.transform.position = new Vector3(106, 68, 15);
MainCamera.transform.rotation = new Vector4(40, 145, 0);
}
As usual many thanks
UPDATED WITH CODE THAT SOLVED MY PROBLEM.
public Camera MainCamera;
public GameObject TargetPosition;
public int speed = 2;
bool camera_move_enabled = false;
void Update()
{
if (camera_move_enabled)
{
MainCamera.transform.position = Vector3.Lerp(transform.position, TargetPosition.transform.position, speed * Time.deltaTime);
MainCamera.transform.rotation = Quaternion.Lerp(transform.rotation, TargetPosition.transform.rotation, speed * Time.deltaTime);
}
}
public void UserClickedCameraResetButton()
{
TargetPosition.transform.position = new Vector3(-106.2617f, 68.81419f, 14.92558f);
TargetPosition.transform.rotation = Quaternion.Euler(39.7415f, 145.0724f, 0);
camera_move_enabled = true;
}
Upvotes: 4
Views: 15149
Reputation: 168
Try this, set your transform position and rotation to a targetposition object. since the transition has to be done over multiple frames you have to put this under update method. you can set an variable to enable or disable the camera movement. just change the targetposition and enable the camera movement to move the camera.
public GameObject Targetposition;
public bool camera_move_enabled;
void Update () {
if(camera_move_enabled){
Maincamera.transform.position = Vector3.Lerp (transform.position, Targetposition.transform.position, speed * Time.deltaTime);
Maincamera.transform.rotation = Quaternion.Lerp (transform.rotation, Targetposition.transform.rotation, speed * Time.deltaTime);
}
}
public void UserClickedCameraResetButton()
{
Targetposition.transform.position = new Vector3(106, 68, 15);
Targetposition.transform.rotation = new Vector4(40, 145, 0);
camera_move_enabled = true;
}
disable the camera_move_enabled after transition. change the speed value(float) to faster/slower transition.
i'm also new to unity so this may be not the best solution :)
Upvotes: 4