- Home /
Elevator script problem
I recently made this elevator script, and it works great most of the time. However, sometimes when you get to the top it loops (jumps to the bottom and moves back up again). I've looked at the code but can't seem to find the reason why this happens. If you can find out way this is happening, please let me know. Thanks!
public Vector3 pointB;
Vector3 startpoint;
public float delay;
public float speed;
GameObject player;
bool hasMoved;
float playerPos;
float thisPos;
IEnumerator Start ()
{
player=GameObject.FindGameObjectWithTag("Player");
startpoint=transform.position;
hasMoved=false;
while (true)
{
yield return StartCoroutine (PlayerTracking());
}
}
IEnumerator OnCollisionEnter(Collision col)
{
if (!hasMoved)
{
yield return StartCoroutine("Move",delay);
}
}
IEnumerator PlayerTracking ()
{
playerPos=player.transform.position.x;
thisPos=gameObject.transform.position.x-transform.localScale.x/2;
if (playerPos<thisPos && transform.position==pointB)
{
yield return StartCoroutine ("MoveBack");
}
}
IEnumerator Move ()
{
GameObject.FindGameObjectWithTag ("Player").SendMessage ("MovingUp");
var i= 0.0f;
var rate= 1.0f/speed;
while (i < 1.1f)
{
i += Time.deltaTime * rate;
transform.position=Vector3.Lerp(startpoint,pointB,i);
yield return null;
}
if(transform.position==pointB)
{
GameObject.FindGameObjectWithTag ("Player").SendMessage ("DoneMoving");
hasMoved=true;
StopCoroutine ("Move");
}
}
IEnumerator MoveBack ()
{
var i= 0.0f;
var rate= 1.0f/speed;
while (i < 1.0f)
{
i += Time.deltaTime * rate;
transform.position=Vector3.Lerp(pointB,startpoint,i);
yield return null;
}
if (transform.position==startpoint)
{
hasMoved=false;
StopCoroutine ("MoveBack");
}
}
IEnumerator Death ()
{
yield return StartCoroutine("MoveBack");
}
}
Answer by Naphier · Apr 26, 2015 at 05:29 AM
This is some interesting structure you've got going here. I'm confused as to why everything is IEnumerators. Unity's engine may have a problem with making an IEnumerator Start(), might... I think PlayerTracking shouldn't be an IEnumerator, but just a void and should be called in an Update function. GameObject.FindGameObjectWithTag ("Player") is slow and should not be called repetitively if not needed, I'd just call once in the Start function and store the player go in a private variable. Beyond simple restructuring / optimizations I can't specifically see something wrong with the code. I'd suggest adding some debug.log messages in places to determine what is actually happening with this script.
Your answer
Follow this Question
Related Questions
Making a bubble level (not a game but work tool) 1 Answer
How do I setup fixed movement between predetermined tiles? 0 Answers
GetKeyUp not registering 2 Answers
Basic collision not working - c# 1 Answer
Help with elevator Script 2 Answers