Moving a lift from my Roof to a different position near the entrance to my level.

Anyone,

I am making a level and the exit is on the roof. After I finish I need to enter a lift and have it travel to a position on the ground near the entrance to my level to exit. I have an elevator script that moves the elevator from position 1 to position 2 but only up and down. I was hoping I could use it to make the lift move from point A to point B. But I can only make it go up or down. I about 125 lines of code that I was hoping I could change around a bit to make it work. Any help would be great! Please.

using UnityEngine;

public class Exit_Lift : MonoBehaviour {

    //variable to assign the message system to
    private MessageSystem msg;

    //int for which floor we need to go to
    public int goToRmNum = 1;
    //int for what floor we are currently on
    private int curRmNum = 1;

    //two public transforms that can be used for room positions
    public Transform rmPos1;
    public Transform rmPos2;

    //two public strings for the names of our floors
    public string floor1name = "";
    public string floor2name = "";

    //how fast the elevator moves
    public float speed = 1.0f;

    //how close we need to be to a floor before we stop the elevator
    public float snapDist = 0.1f;

    void Start()
    {
        msg = GameObject.FindObjectOfType<MessageSystem>();
    }

    void OnTriggerStay(Collider other)
    {
        if(other.tag == "Player")
        {
            if(curRmNum == 1)
                msg.NewMessage("Elevator", "Welcome to the <b>" + floor1name + "</b> floor./n/nPress <b>E</b> to INTERACT with the elevator.", 3.0f);
            else if(curRmNum == 2)
                msg.NewMessage("Elevator", "Welcome to the <b>" + floor2name + "</b> floor./n/nPress <b>E</b> to INTERACT with the elevator.", 3.0f);

            if (Input.GetKeyDown(KeyCode.E) && curRmNum == 2)
            {
                goToRmNum = 1;
            }
            else if(Input.GetKeyDown(KeyCode.E) && curRmNum == 1)
            {
                goToRmNum = 2;
            }
        }
    }

    //once every frame
    void Update()
    {
        //if the go to room is greater than our current room
        if(goToRmNum > curRmNum)
        {
            //move UP
            MoveUp();
        }
        //if the go to room is less than our current room
        else if(goToRmNum < curRmNum)
        {
            //move DOWN
            MoveDown();
        }
        else //neither less than or greater than?
        {
            //don't move
        }

        //if our room is 1
        if(goToRmNum == 1)
        {
            //get the distance between where we are and where the first room position is
            float dist = Vector3.Distance(rmPos1.position, transform.position);

            //if our distance is less than or equal to our snap distance
            if(dist <= snapDist)
            {
                //set our current room
                curRmNum = 1;
                //set our transform position to room 1 position
                transform.position = rmPos1.position;
            }
        }
        //if our room is 2
        else if (goToRmNum == 2)
        {
            //get the distance between where we are and where the second room position is
            float dist = Vector3.Distance(rmPos2.position, transform.position);

            //if our distance is less than or equal to our snap distance
            if (dist <= snapDist)
            {
                //set our current room
                curRmNum = 2;
                //set our transform position to room 1 position
                transform.position = rmPos2.position;
            }
        }
    }

    //custom function to move up
    void MoveUp()
    {
        //move the elevator up the Y axis
        Vector3 newPos = transform.position;
        newPos.y += speed * Time.deltaTime;

        //set our position equal to the new Position
        transform.position = newPos;
    }

    //custom function to move down
    void MoveDown()
    {
        //move the elevator down the Y axis
        Vector3 newPos = transform.position;
        newPos.y -= speed * Time.deltaTime;

        //set our position equal to the new Position
        transform.position = newPos;
    }
}

You probably want to use some kind of tweening package (iTween, GoTween, etc… there are TONS of free ones) to define the path you want, then use your above script to “drive” the elevator along the tween curve. That way you have a general purpose solution for any level layout you want.

I got a different way of doing it. The code below allows me to move the lift from point a to b successfully. Though its on a loop and I only need to use it once since its for the exit.

using UnityEngine;

public class Exit_Lift : MonoBehaviour {

    public Transform[] points;
    private int pointMarker = 0;
    private Transform currentPoint;
    private Vector3 lastPoint;
    private bool forward = true;
    public float speed = 1.0f;
    private float startTime;
    private float journeyLength;

    void Start ()
    {
        startTime = Time.time;
        lastPoint = transform.position;
    }

    // Update is called once per frame
    void Update () {

        if (forward)
        {
            currentPoint = points[pointMarker];
            journeyLength = Vector3.Distance(lastPoint, currentPoint.position);

            float distCovered = (Time.time - startTime) * speed;
            float fracJourney = distCovered / journeyLength;

            if (transform.position != currentPoint.transform.position)
            {
                transform.position = Vector3.Lerp(lastPoint, currentPoint.position, fracJourney);
            }
            else
            {
                // Get new time for next loop
                startTime = Time.time;
                lastPoint = transform.position;

                if (pointMarker != points.Length -1)
                    pointMarker++;
                else
                    forward = false;
            }
        }
        else
        {
            // go in reverse
            currentPoint = points[pointMarker];
            journeyLength = Vector3.Distance(lastPoint, currentPoint.position);

            float distCovered = (Time.time - startTime) * speed;
            float fracJourney = distCovered / journeyLength;

            if (transform.position != currentPoint.transform.position)
            {
                transform.position = Vector3.Lerp(lastPoint, currentPoint.position, fracJourney);
            }
            else
            {
                // Get new time for next loop
                startTime = Time.time;
                lastPoint = transform.position;

                if (pointMarker != 0)
                    pointMarker--;
                else
                    forward = true;
            }
        }
    }
}