- Home /
Question by
BuzzardMercure · Jul 30, 2015 at 07:49 PM ·
scripting problemcoroutinelerp
Lerp coroutine not executing
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;
}
}
}
Comment
Best Answer
Answer by Dave-Carlile · Jul 30, 2015 at 07:50 PM
You start a coroutine using StartCoroutine, so:
StartCoroutine(elementSlide(bgTarget, startPos, newPos, transTime));
facepalm
I've been so focused on the coroutine itself. Now I'm embarassed.
$$anonymous$$uch gratitude!
Your answer
Follow this Question
Related Questions
[HELP] How to make camera smooth change position while follow object? 1 Answer
C# | How to delay a method with parameters 2 Answers
How to start a Coroutine in another script? 1 Answer
object's children not moving with it when using a Lerp-ing coroutine 0 Answers
Unity freezing on Waitforsecond 1 Answer