Starting animation from another gameobject

I’m trying to start an animation on a door by clicking on another object. Therefore the script is on the object that I click on. How do I start an animation on an object using a script on another object?

I posted this script in another’s forum. but it probably is what you need to a degree.

This can easily control another object without the use of animations. Doors are either slides or rotates so tinkering with the code could give you a number of different activities.

Also, you could put in a timed close as well. So doors are not left open.

// move distance over duration
var distance=Vector3(5,0,0);
var duration=3.0;

private var moving=false;
private var direction=false;
private var amount=0.0;
private var startTime = 0.0;
private var startPosition : Vector3;

function Start(){
	// store the start position for use in the script
	startPosition=transform.position;
}

function Update(){
	if(moving){
		// lerp according to the direction.
		var timer=(Time.time - startTime) / duration;
		if(direction){
			amount=Mathf.Lerp(1,0,timer);
		}else{
			amount=Mathf.Lerp(0,1,timer);
		}
		
		// set the transform.position to the start position + the distance * the current amount
		transform.position=startPosition + distance * amount;
		
		// check to see if we have reached our direction goal
		var hasEnded=(amount >= 1  !direction) || (amount <= 0  direction);
		if(hasEnded){
			// if so, stop moving and change the direction to the opposite way
			moving=false;
			direction=!direction;
		}
	}
}

function OnMouseDown(){
	if(!moving){
		moving=true;
		startTime=Time.time;
	}
}

thanks alot! But wouldn’t it be easier to start an animation? Since I only see the door once, I don’t need to be able to close it.

For simple, one-shot animation crossfades I generally put a script on the animated object that has something like:

function SetAnimation(animName : String){
	animation.CrossFade[animName];
}

Then I would just use BroadcastMessage/SendMessage on the object (door, in your case) when applicable.