I’m trying to slide an element to a new position. When ‘changeState’ executes, it starts the ‘elementSlide’ coroutine, but nothing happens. I’ve tried writing the coroutine a few different ways, but have ended up with the same problem.
using UnityEngine;
using System.Collections;
public class MindManager : MonoBehaviour {
//Variable to determine if the layer is active
public bool currentlyActive = true;
//Variable to determine if the layer is available
public bool currentlyAvailable = true;
//
[System.Serializable]
public struct bgPosScale
{
public float xpos;
public float ypos;
public float scaleFactor;
}
public bgPosScale[] bgOffset = new bgPosScale[3];
public GameObject bgTarget;
public float bgScale;
public float transTime = 100f;
//Enumerate the subsections of the Mind layer
public enum SUBSTATE : int {TOP =0, HEAD = 1, LEFT = 2, RIGHT = 3,};
public SUBSTATE subState = SUBSTATE.TOP;
public void Start()
{
}
public void changeState(int toState)
{
if (toState != (int)subState) //Only transition if state is not currently active
{
Vector3 startPos = bgTarget.transform.position;//Store BG element's current position
Vector3 newPos = new Vector3(bgOffset[toState].xpos, bgOffset[toState].ypos,0);//Grab the desired position
subState = (SUBSTATE)toState; //Set desired state to active state
elementSlide(bgTarget, startPos, newPos, transTime); //Initiate coroutine
//bgTarget.transform.position = new Vector3 (bgOffset[toState].xpos, bgOffset[toState].ypos,0);
//bgTarget.transform.localScale = new Vector3(bgScale*bgOffset[toState].scaleFactor,bgScale*bgOffset[toState].scaleFactor,0);
}
}
public IEnumerator elementSlide (GameObject target, Vector3 startPos, Vector3 newPos, float time)
{
float timePassed = 0f;
while (timePassed < time) {
target.transform.position = Vector3.Lerp (startPos, newPos, (timePassed / time));
timePassed += Time.deltaTime;
yield return null;
}
}
}