- Home /
Making Camera Lerp back to original position
Hello All,
I am Lerping a camera away from its FPC, exploring a bit and then I want to move it back to it's Original FPC
Here's my Code:
Works now: Zoom In:
var target: Transform;
var camPos: Vector3;
var endPos: Vector3;
var time : float;
function OnMouseDown () {
camPos = Vector3( Camera.main.transform.position.x, Camera.main.transform.position.y, Camera.main.transform.position.z);
endPos = Vector3( target.transform.position.x, target.transform.position.y, target.transform.position.z);
var i = 0.0;
var rate = 1.0/time;
while (i < 1.0) {
i += Time.deltaTime * rate;
Camera.main.transform.position = Vector3.Lerp(camPos, endPos, i);
yield;
}
}
Zoom Out:
var camPos: Vector3;
var endPos: Vector3;
var time : float;
function OnMouseDown () {
endPos = Vector3( Camera.main.transform.position.x, Camera.main.transform.position.y, Camera.main.transform.position.z);
var i = 0.0;
var rate = 1.0/time;
while (i < 1.0) {
i += Time.deltaTime * rate;
Camera.main.transform.position = Vector3.Lerp( endPos, camPos, i);
yield;
}
}
I was hard coding Vector3's to get it near it's original position (& where the FPC is still) But throughout the gameplay The Cam & FPC became more and more separated which leads to problems Thanks
~ be
Don't think that something like that should happen unless your not returning the camera back to its original position...
also, ins$$anonymous$$d of
camPos = Vector3( Camera.main.transform.position.x, Camera.main.transform.position.y, Camera.main.transform.position.z);
why not just do
camPos = Camera.main.transform.position;
?
That won't fix the problem, but looks nicer as the result is the same.. as for the problem..
any chance that you are moving the object while zoo$$anonymous$$g? as in.. you move the character while the camera is zoo$$anonymous$$g in/out so by the time the camera returns to "camPos" the character isn't actually at that spot anymore..
Thanks ShadoX
There is no character, just just the Camera & the First Person Controller Pill. When I Lerp the Camera it leaves the FPC to do so and I just want to get it back to the FPC at the end.
Something like (although not proper js) :
function on$$anonymous$$ouseDown () {
Camera.main.transform.Position = FirstPersonConto;;er.transform.position
}
Thanks
~be
Answer by Bentoon · Mar 19, 2014 at 08:50 PM
Hello All,
Here's what I found to work that both puts the Camera Back to the FPController and also lerps it more specifically to where you click initially:
Zooom In script:
var target: Transform;
var controller : GameObject;
var camPos: Vector3;
var endPos: Vector3;
var time : float;
function Update () {
var controller : CharacterController = GetComponent(CharacterController);
}
function OnMouseDown () {
camPos = Vector3( controller.transform.position.x, controller.transform.position.y, controller.transform.position.z);;
endPos = Vector3( target.transform.position.x, target.transform.position.y, target.transform.position.z);
var i = 0.0;
var rate = 1.0/time;
while (i < 1.0) {
i += Time.deltaTime * rate;
Camera.main.transform.position = Vector3.Lerp(camPos, endPos, i);
yield;
}
}
THEN Zoom Out Script :
var target: Transform;
var controller : GameObject;
var camPos: Vector3;
var endPos: Vector3;
var time : float;
function Update () {
var controller : CharacterController = GetComponent(CharacterController);
}
function OnMouseDown () {
endPos = Vector3( Camera.main.transform.position.x, Camera.main.transform.position.y, Camera.main.transform.position.z);
camPos = Vector3( controller.transform.position.x, controller.transform.position.y, controller.transform.position.z);;
var i = 0.0;
var rate = 1.0/time;
while (i < 1.0) {
i += Time.deltaTime * rate;
Camera.main.transform.position = Vector3.Lerp( endPos, camPos, i);
yield;
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613140624im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How to smooth my camera (Vector3.Lerp) 0 Answers
(Vector3.Lerp) Camera doesn't reach the specified Destination! 1 Answer
Zooming camera using move z position. 0 Answers
Camera Lerp from A to B, and back 2 Answers
Need to flatten a Vector3 2 Answers