Lerp on the every 3 * n cycle does some wierd thing.
To fill you in on the details, i have a tower set from two pieces. The upper piece is floating a top the bottom one. There is a third part which is invisible and basically works as a lerp destination. So the problem is:
2nd part goes to the 3rd part smoothly
Goes down to its starting position smoothly.
When the percent is 100% it should go to top part again, but it leaps to mid of the animation, goes back to starting point and does the two next steps smoothly again before again leaping for a split second.
Here is the code :
using UnityEngine;
using System.Collections;
public class TowerUpDownEffect : MonoBehaviour
{
public float speed = 0.1f;
public GameObject MovingTowerPart;
public GameObject Staaa;
public Vector3 MovingTowerPartVector3down;
public Vector3 MovingTowerPartVector3up;
private float Percent = 0;
private bool doOnce = true;
private int Switch = 1;
// Use this for initialization
void Start()
{
MovingTowerPartVector3down = MovingTowerPart.transform.position;
MovingTowerPartVector3up = Staaa.transform.position;
}
// Update is called once per frame
void Update()
{
if (Switch == 1)
{
if (Percent > 1)
{
Switch = 2;
Percent = 0;
// MovingTowerPart.transform.position = MovingTowerPartVector3up;
}
MovingTowerPart.transform.position = Vector3.Lerp(MovingTowerPartVector3down, MovingTowerPartVector3up, Percent = Percent + (Time.deltaTime * speed));
Debug.Log(Percent);
}
if (Switch == 2)
{
if (Percent > 1)
{
Switch = 1;
Percent = 0.04f;
// MovingTowerPart.transform.position = MovingTowerPartVector3down;
}
MovingTowerPart.transform.position = Vector3.Lerp(MovingTowerPartVector3up, MovingTowerPartVector3down, Percent = Percent + (Time.deltaTime * speed));
Debug.Log(Percent);
}
}
}
The album of the problem. You can see that between 3-4-5 image it leaps to mid and start again at bottom. It does this every time it goes to bottom and goes up starting from 0 percent.
I have tried setting Percent to 0, 0.04. or even 0.1 and it still does it.