Hi all.
I made an script so that menu items do appear smoothly. I’m able to make the code run within update but unable to make it run within a coroutine and I’m unable to find the issue. Anyone can see what is wrong?
using UnityEngine;
using System.Collections;
public class MenuLerp : MonoBehaviour {
GameObject objToMove;
float delay;
public float lerpTime = 1f;
float currentLerpTime;
public float moveDistance = 10f;
Vector3 startPos;
Vector3 endPos;
public bool inwards;
public bool outwards;
public enum ComingFrom{up, down, right, left};
public ComingFrom direction;
/*
public bool up;
public bool down;
public bool right;
public bool left;
*/
// Use this for initialization
void Start () {
if (inwards == true){
endPos = transform.position;
switch (direction){
case ComingFrom.up:
startPos = transform.position + transform.up * moveDistance;
break;
case ComingFrom.down:
startPos = transform.position + -transform.up * moveDistance;
break;
case ComingFrom.left:
startPos = transform.position + transform.right * moveDistance;
break;
case ComingFrom.right:
startPos = transform.position + -transform.right * moveDistance;
break;
}
//StartCoroutine (test());
//startPos = transform.position + transform.up * moveDistance;
//EL OUTWARDS MIENTRAS EL LERP ESTE EN UPDATE, PUES NADA.
}
if (outwards == true){
startPos = transform.position;
endPos = transform.position + transform.up * moveDistance;
}
}
/*
protected void Start() {
startPos = transform.position;
endPos = transform.position + transform.up * moveDistance;
}
*/
protected void Update() {
//reset when we press spacebar
if (Input.GetKeyDown(KeyCode.Space)) {
currentLerpTime = 0f;
}
//increment timer once per frame
currentLerpTime += Time.deltaTime;
if (currentLerpTime > lerpTime) {
currentLerpTime = lerpTime;
}
//lerp! // ease In
if (inwards==true){
float t = currentLerpTime / lerpTime;
t = Mathf.Sin(t * Mathf.PI * 0.5f);
transform.position = Vector3.Lerp(startPos, endPos, t);
}
//lerp! // ease out
if (outwards==true){
float t = currentLerpTime / lerpTime;
t = 1f - Mathf.Cos(t * Mathf.PI * 0.5f);
transform.position = Vector3.Lerp(startPos, endPos, t);
}
}
/*
IEnumerator test(){
currentLerpTime = 0f;
lerpTime = 40f;
float t = 0f;
while ( t <lerpTime)
{
t = currentLerpTime / lerpTime;
currentLerpTime += Time.deltaTime;
//t = 1f - Mathf.Cos(t * Mathf.PI * 0.5f);
transform.position = Vector3.Lerp( new Vector3(0,500,0), new Vector3(0,0,0), t);
Debug.Log ("t" + t);
}
yield return new WaitForEndOfFrame();