Hello,
This is probably a simple answer that I just can’t seem to find. Everything works fine in the following code, except upon pressing the tab key too fast, the movement hangs in the middle of the lerp and can’t continue its movement. I read somewhere that using Time.deltaTime isn’t necessary, but I don’t know what to substitute and keep the movement smooth.
IEnumerator FocusCoroutine (Transform target)
{
while (Vector3.Distance (transform.position, target.position) > 0.025f)
{
transform.position = Vector3.Lerp (transform.position, target.position, MoveSpeed * Time.deltaTime);
transform.parent = target.transform;
yield return null;
}
Debug.Log ("Target Reached!");
}
// called by pressing TAB key from another script
public void SwitchActive()
{
if (TargetHumanEnabled == true)
{
TargetHumanEnabled = false;
TargetAnimalEnabled = true;
SwitchFocus ();
}
else if (TargetAnimalEnabled == true)
{
TargetAnimalEnabled = false;
TargetHumanEnabled = true;
SwitchFocus ();
}
}
// called after SwitchActive method and once at Start of script
// calls FocusCoroutine
void SwitchFocus()
{
Debug.Log ("Tab Key Pressed");
GameObject targetLookAtHuman;
GameObject targetLookAtAnimal;
targetLookAtHuman = GameObject.Find ("targetLookAtHuman") as GameObject;
targetLookAtAnimal = GameObject.Find ("targetLookAtAnimal") as GameObject;
if (TargetAnimalEnabled == true)
{
target = (targetLookAtAnimal.transform);
Debug.Log ("Animal Enabled");
StartCoroutine (FocusCoroutine (target));
}
else if (TargetHumanEnabled == true)
{
target = (targetLookAtHuman.transform);
Debug.Log ("Human Enabled");
StartCoroutine (FocusCoroutine (target));
}
}
}