Quantcast
Channel: Latest Questions by Macdude2
Viewing all articles
Browse latest Browse all 38

Animate Doors and Button Through Script

$
0
0

Alright, I have tried to create doors, a button, and a collider to trigger the button in order to make it so that if a player walks onto a button, it will open the doors. However, I would like the opening and closing to be animated with script and not an actual animation because I will be using many doors throughout my game and I do not think it is possible to make an animation non-position specific. To accomplish this I have tried to use lerp. This works when it goes without being activated, but when it is activated, the doors and button do not animate, but instead instantaneously change position. The script I use on the doors and button is below and the script I use on the button activator is below that. Thanks for any help.

* Code is fixed and now animates over several frames.

var start : Transform;
var end : Transform;
var ObjectSpeed = 1.0;
private var Down = 0;
private var startTime : float;

function ButtonDown() {
    startTime = Time.time;
    Down = 1;
    print("button Down");
}

function ButtonUp() {
    Down = 2;
    print("button Up");
}

function Update(){

        var dist = Vector3.Distance(start.position, end.position);
        var elapsedTime = Time.time - startTime;

    if(Down == 1){
            transform.position = Vector3.Lerp(start.position, end.position,elapsedTime*ObjectSpeed);

             if (dist <= 0)
              Down = 0;
        }

    if(Down == 2){
            transform.position = Vector3.Lerp(end.position, start.position, elapsedTime*ObjectSpeed);

             if (dist <= 0)
                  Down = 0;
    }
}

This is the button activator script:

var RightDoor: Transform;
var LeftDoor: Transform;
var MyButton: Transform;

function OnTriggerEnter (other: Collider)
{
    if(other.gameObject.CompareTag("Player") || other.gameObject.CompareTag("Box"))
{
    print("yes");
            MyButton.gameObject.SendMessage("ButtonDown", null);
            LeftDoor.gameObject.SendMessage("ButtonDown", null);
            RightDoor.gameObject.SendMessage("ButtonDown", null);
}
}

function OnTriggerExit (other: Collider)
{

    if(other.gameObject.CompareTag("Player") || other.gameObject.CompareTag("Box"))
{
            MyButton.gameObject.SendMessage("ButtonUp", null);
            LeftDoor.gameObject.SendMessage("ButtonUp", null);
            RightDoor.gameObject.SendMessage("ButtonUp", null);
}
}

Viewing all articles
Browse latest Browse all 38

Trending Articles



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