- Home /
Smooth change of camera position goes wrong
have this code to smoothly change the camera position:
public GameObject MyCam;
public float strength;
Vector3 cameraPosition = new Vector3(22.44445f, -3, 4.542897f);
MyCam.transform.position = Vector3.Lerp(transform.position, cameraPosition, strength);
however it sends my camera to absolutely different position(for example, 3.008285,-9.02228,-12.27002) - I read that it depends some from the 3rd parameter (strength in my case, I tested it setting from 0,001 and up to 10 - and still not wanted result)
Answer by sethuraj · Jul 18, 2014 at 11:35 AM
Lerp is the short form of Linear Interpolation.Its used to linearly interpolate between two values,in this case two vector points.
Syntax of lerp for Vector3 is
Vector3.Lerp(Vector_One,Vector_Two,LerpValue)
The lerp value ranges from 0 to 1. No use in giving 10 or 100.
1) If lerp value is zero result is Vector_One.
2) If lerp value is one then result is Vector_Two.
3) If Lerp is 0.5f then result is in between Vector_One and Vector_Two
4) Lerp value should vary from 0 to one inorder to get the smooth transition
public GameObject MyCam;
public float strength=0.0f;
Vector3 InitialPosition;
Vector3 FinalPosition;
void Start()
{
InitialPosition=transform.position;
FinalPosition=new Vector3(22.44445f, -3, 4.542897f);
}
void Update()
{
if(strength<1.0f)
{
strength+=Time.deltatime;
}
MyCam.transform.position=Vector3.Lerp(InitialPosition,FinalPosition,strength);
}
thanks for reply, it became smooth but I still don't understand why position of the camera in the end(when strength is 1 - 22.44445, -12.55874, 5.470654) is absolutely different from FinalPosition(22.44445f, -3, 4.542897f). The only axis that works correctly is X - it remains 22.44445f
Your answer
Follow this Question
Related Questions
C# Check If Scripted Gameobject goes Past Variable Gameobject 0 Answers
Rotate camera smoothly for particular position with single INPUT 0 Answers
How do I check if an Object isn't moving? (C#) 1 Answer
Positioning a GameObject so I't wont be inside another object 1 Answer
C# sticking to Cube corners position (raycasting AI script) 1 Answer