Quantcast
Channel: Answers by "Motionreactor"
Viewing all articles
Browse latest Browse all 32

Answer by Motionreactor

$
0
0

Ok, I've written a complete script for you because I thought it important to help illustrate a more flexible approach to the problem:

Drop this script onto the elevator:

var elevatorSpeed : float;
var floor1Height : float;
var floor2Height : float;
var floor3Height : float;

private var elevator : GameObject;
private var elevatorActive : boolean;
private var elevatorDestination : float;

function Start()
{
    // initialise variables which must have a value, otherwise errors ensue
    elevator = this.gameObject;
    elevatorActive = false;
    elevatorDestination = elevator.transform.position.y;
}

function Update() {
    if(elevatorActive)
    {
        MoveElevator();
    }
}

function MoveElevator()
{
    // Move the elevator
    elevator.transform.position.y = Mathf.Lerp(elevator.transform.position.y, elevatorDestination, elevatorSpeed * Time.deltaTime);

    // Disable the elevator if it is within a very small margin of the destination, ie it is 'close enough' so stop
    if(Mathf.Abs(elevator.transform.position.y - elevatorDestination) < 0.01)
    {
        print("stopped elevator");
        elevatorActive = false;
    }
}

function GoToFloor(floorNum : int)
{
    switch(floorNum)
    {
        case 1:
            elevatorDestination = floor1Height;
            break;
        case 2:
            elevatorDestination = floor2Height;
            break;
        case 3:
            elevatorDestination = floor3Height;
            break;
        default:
            elevatorDestination = floor1Height;
            break;
    }

    elevatorActive = true;
}

function StopElevator()
{
    elevatorActive = false;
}

function ResumeElevator()
{
    elevatorActive = true;
}

Now all you have to do is set the speed and floor variables in the component.

To 'trigger' the elevator from another script on another object, such as a trigger of course:

GameObject.Find("Elevator").GetComponent("ElevatorScript").GoToFloor(1);

...and of course emergency stop:

GameObject.Find("Elevator").GetComponent("ElevatorScript").StopElevator();

...and resume from stopped position (even if halfway between floors):

GameObject.Find("Elevator").GetComponent("ElevatorScript").ResumeElevator();

Voila! ...and all without a pesky while() loop in sight! (nasty things for beginners!)

An alternative way to move the elevator with a clamped speed, limited to a maximum is a case of changing a variable:

var elevatorSpeed : float;

becomes:

var elevatorMaxSpeed : float;

...and the move function :

    function MoveElevator()
    {
        // Calculate difference between current position and desired position
        var elevatorVelocity = elevatorDestination - elevator.transform.position.y;

        // Limit the velocity by clamping it to positive or negative maximum velocity
        elevatorVelocity = Mathf.Clamp(elevatorVelocity, -elevatorMaxSpeed, elevatorMaxSpeed);

        // Move the elevator
        elevator.transform.position.y += elevatorVelocity * Time.deltaTime; // += accomodates positive or negative movement and Time.deltaTime gives us some smoothing

        // Disable the elevator if it is within a very small margin of the destination, ie it is 'close enough' so stop
        if(Mathf.Abs(elevator.transform.position.y - elevatorDestination) < 0.01)
        {
            print("stopped elevator");
            elevatorActive = false;
        }
    }

I set the max speed to a fairly low value 0.5 in my case.


Viewing all articles
Browse latest Browse all 32

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>