- Home /
Camera.rect lerp not working
hi I trying to animate the camera normalised viewport rectangle on the click of a button but it moves without regarding the lerp function i have put in there here is the code:
using UnityEngine;
using System.Collections;
public class moveCam : MonoBehaviour {
public float open;
public float closed;
void OnClick ()
{
Camera.main.rect = new Rect (0, 0, Mathf.Lerp (open, closed, Time.time), 1f);
}
}
Any help is greatly appreciated thanks
Please post comments as comments, not as new answers.
Answer by Seth-Bergman · Jul 23, 2013 at 09:39 PM
you are only calling this for one frame, so it will jump to the precise size determined by that exact Time.time..
first, have a look here:
http://docs.unity3d.com/Documentation/ScriptReference/Mathf.Lerp.html
in the example, in the update function, we are returning a slightly different value each frame for "Time.time". since it is called every frame, the effect is animated over time.. There are plenty of ways to accomplish what you want..
here's one:
float stepper = 0.0f;
bool clicked = true;
float speed = .01f;
void Update ()
{
if(clicked && stepper < open){
Camera.main.rect = new Rect (0, 0, Mathf.Lerp (open, closed, stepper), 1f);
stepper+= speed;
}
else if(!clicked && stepper > closed){
Camera.main.rect = new Rect (0, 0, Mathf.Lerp (closed, open, stepper), 1f);
stepper-= speed;
}
if(Input.GetMouseButtonDown(0))
clicked = !clicked;
}
just an example
If this resolved your issue, accept and vote up the answer.
@trelobyte I voted up your question so you should have enough karma now to vote up this answer. It is always good to vote up the answer you accepted.
Answer by jamesflowerdew · Mar 07, 2015 at 12:28 PM
This might be helpful :)
am just writing in the lerp not using mathf, but it works, and as a function .
Rect LerpRect(Rect nRect1, Rect nRect2,float nNum){
Rect vRect=nRect1;
vRect.x+=(nRect2.x-nRect1.x)*nNum;
vRect.y+=(nRect2.y-nRect1.y)*nNum;
vRect.width+=(nRect2.width-nRect1.width)*nNum;
vRect.height+=(nRect2.height-nRect1.height)*nNum;
return(vRect);
}
Your answer
Follow this Question
Related Questions
Zoom camera to correct FoV based on Rect size 1 Answer
How to move camera from point A to B 1 Answer
How to make camera position relative to a specific target. 1 Answer
Camera Lerp Jitter 0 Answers
3D menu camera rotation issue. 0 Answers