- Home /
slow a lerp ? array/static problem
basically im using this to snap between 4 different zoom levels of a camera, that works fine, now i can't get my lerp to work gradually or be "smooth" any insight would be awesome,
i know im kinda of asking people to do it for me, but i just cannot figure out as this is new territory for me, thanks in advance :)
#pragma strict
var target : Transform ;
var distance:float = 20.0;
var switchDistance : int;
var height : float = 10.0;
var rotationDamping : float = 1.0;
var rotateSpeed : float = 20.0;
var speed : float = 0.001;
private var heightArray : float[];
private var lengthArray : float[];
private var distanceArray : float[];
var i :int;//indexes our array values by assigning them a number in our switch case
function Start () {
heightArray = [10.0, 20.0 ,30.0,40.0];
lengthArray = [10.0, 20.0 ,30.0,40.0];
distanceArray = [10.0, 100.0 ,150.0,400.0];
switchDistance = 2.0;
}
function Update () {
distance = distanceArray[i]; //here we actually set i as part of our array
if(Input.GetButton("rightMouse")){
var h : float = rotateSpeed * Input.GetAxis ("Mouse X");
transform.Rotate (0, h, 0);
}
switchDistance= Mathf.Clamp(switchDistance,0.0,4.0);
distance = distanceArray[i];
height = heightArray[i];
}
function LateUpdate() {
var wantedRotationAngle = target.eulerAngles.y;
var wantedHeight = target.transform.position.y + height;
var currentRotationAngle = transform.eulerAngles.y;
var currentHeight = transform.position.y;
if (Input.GetAxis("mouseZoom")>0 && switchDistance <=4 ){
Debug.Log("+");
switchDistance -= 1 ;
}
if (Input.GetAxis("mouseZoom")<0 && switchDistance >=0){
switchDistance += 1 ;
}
switch (switchDistance){
case 3:
//distance = 75.0;height = 15; break;
//transform.position.x=Mathf.Lerp(heightArray[3],heightArray[3],Time.time);Debug.Log("case3");break;
i= 3;transform.position.x=Mathf.Lerp(distanceArray[3],distanceArray[3],Time.time*speed);Debug.Log("case3Lngth");break;
case 2:
//transform.position.x=Mathf.Lerp(heightArray[2],heightArray[3],Time.time);Debug.Log("case2");break;
i=2;transform.position.x=Mathf.Lerp(distanceArray[2],distanceArray[3],Time.time*speed);Debug.Log("case2Lngth");break;
case 1:
//transform.position.x=Mathf.Lerp(heightArray[1],heightArray[2],Time.time);Debug.Log("case1");break;
i=1;transform.position.x=Mathf.Lerp(distanceArray[1],distanceArray[2],Time.time*speed);Debug.Log("case1Lngth");break;
case 0:
//transform.position.x=Mathf.Lerp(distanceArray[0],distanceArray[1],Time.time);Debug.Log("case0");break;
//transform.position.z=Mathf.Lerp(lengthArray[0],lengthArray[1],Time.time);Debug.Log("case0Lngth");break;
i= 0;transform.position.x=Mathf.Lerp(heightArray[0],heightArray[1],Time.time*speed);Debug.Log("case0Lngth");break;
}
currentRotationAngle = Mathf.LerpAngle(currentRotationAngle,wantedRotationAngle,rotationDamping*Time.deltaTime);
var currentRotation = Quaternion.Euler(0,currentRotationAngle,0);
transform.position = target.position;
transform.position -= currentRotation*Vector3.forward*distance;
transform.position.y = wantedHeight;
transform.LookAt (target);
}
Answer by robertbu · Apr 26, 2013 at 02:08 PM
There are two ways you can approach this problem. The first is a "misuse" of Lerp() that is commonly used and results in an easing of the movmenet:
transform.position.x=Mathf.Lerp(transform.position.x,distanceArray[3],Time.deltaTime*speed);
The second is more traditional. It involves you building a timer. The timer gets reset to 0 at the beginning of the movement and then is incremented at every Update() call:
timer += Time.deltaTime;
transform.position.x=Mathf.Lerp(distanceArray[2],distanceArray[3],timer*speed);
Note that the last parameter in the Lerp() call is a fraction between 0 and 1. When the value is 0, it will be at the first position. When the value is 1 it will be at the second position. Except for a brief time at the beginning of the game, Time.time will always be greater than 1.0.
i almost have it working, ended up with something like this, lerp is now at the bottom, cases govern array value only #pragma strict var target : Transform ; var distance:float = 20.0; var switchDistance : int; var height : float = 10.0; var rotationDamping : float = 1.0; var rotateSpeed : float = 20.0; var speed : float = 2.0; private var heightArray : float[]; private var lengthArray : float[]; private var distanceArray : float[]; var i :int;//indexes our array values by assigning them a number in our switch case var timer : float; function Start () { heightArray = [10.0, 20.0 ,30.0,40.0]; lengthArray = [10.0, 20.0 ,30.0,40.0]; distanceArray = [10.0, 100.0 ,150.0,400.0]; switchDistance = 2.0; } function Update () { distance = distanceArray[i]; //here we actually set i as part of our arr timer += Time.deltaTime; if(Input.GetButton("right$$anonymous$$ouse")){ var h : float = rotateSpeed * Input.GetAxis ("$$anonymous$$ouse X"); transform.Rotate (0, h, 0); } switchDistance= $$anonymous$$athf.Clamp(switchDistance,0.0,4.0); distance = distanceArray[i]; height = heightArray[i]; } function LateUpdate() { var wantedRotationAngle = target.eulerAngles.y; var wantedHeight = target.transform.position.y + height; var currentRotationAngle = transform.eulerAngles.y; var currentHeight = transform.position.y; if (Input.GetAxis("mouseZoom")>0 && switchDistance <=4 ){ Debug.Log("+"); switchDistance -= 1 ; } if (Input.GetAxis("mouseZoom")<0 && switchDistance >=0){ switchDistance += 1 ; } switch (switchDistance){ case 3: //distance = 75.0;height = 15; break; //transform.position.x=$$anonymous$$athf.Lerp(heightArray[3],heightArray[3],Time.time);Debug.Log("case3");break; i= 0;break; case 2: //transform.position.x=$$anonymous$$athf.Lerp(heightArray[2],heightArray[3],Time.time);Debug.Log("case2");break; i=1;break; case 1: //transform.position.x=$$anonymous$$athf.Lerp(heightArray[1],heightArray[2],Time.time);Debug.Log("case1");break; i=2;break; case 0: //transform.position.x=$$anonymous$$athf.Lerp(distanceArray[0],distanceArray[1],Time.time);Debug.Log("case0");break; //transform.position.z=$$anonymous$$athf.Lerp(lengthArray[0],lengthArray[1],Time.time);Debug.Log("case0Lngth");break; i= 3; break; } currentRotationAngle = $$anonymous$$athf.LerpAngle(currentRotationAngle,wantedRotationAngle,rotationDamping*Time.deltaTime); var currentRotation = Quaternion.Euler(0,currentRotationAngle,0); transform.position = target.position; transform.position -= currentRotation*Vector3.forward*distance; transform.position.y = wantedHeight; transform.position.x =$$anonymous$$athf.Lerp(transform.position.x,distanceArray[i],Time.deltaTime*speed); transform.LookAt (target); }
Your answer
Follow this Question
Related Questions
Smooth gradient between colours? 1 Answer
How to move along a group of waypoints at a certain speed? 2 Answers
c# wait script 2 Answers
Trying to lerp in different states in a switch statement 0 Answers
Mathf.Lerp Question 2 Answers