- Home /
play only one door animation
Hi folks,
I am current;y building a scene where the player is in a ship, in the ship as you would assume there are doors and the players open them by clicking 'o' on a control panel...the door animation will then play, so the door is open.
The problem is, when I play the animation for some reason it opens every door as oppose to the one the control panel is connected to through scripts.
I have one script connected to the control panel and one connected to the door. In the control panel script it checks to see if the player has pressed O and if they do it sends a message to the door connected to this script, at the start called triggeringObject,
if (Input.GetKeyDown (KeyCode.O))
{
if (activated == true)
{
activated = false;
}
else
{
activated = true;
}
}
if (activated == true)
{
triggeringObject.SendMessage("TurnOn");
}
else
{
triggeringObject.SendMessage("TurnOff");
}
In the doors trigger object script I have to functions one to turn on and one to turn off.
void TurnOn()
{
//this.transform.renderer.material.color = new Color(0,255,0,255);
this.transform.animation.wrapMode = WrapMode.ClampForever;
this.transform.animation.Play("Take 001");
}
void TurnOff()
{
//this.transform.renderer.material.color = new Color(255,0,0,255);
this.transform.animation.Rewind();
}
as you can see I tell it trigger the animation of that door, yet every door gets played. Any Ideas?
Thanks in advanced
A question: In what function is first code snippet you show located?
Answer by SteveFSP · Nov 22, 2012 at 05:04 PM
I don't see how you are filtering what is receiving the key down. If key detection is occurring in the update function of each control panel, then all control panels are detecting the key down and sending out the message to all doors. E.g. 3 control panels have the script attached, 3 will detect the key down and send out the message to 3 doors.
A common technique is to put the key detection into an input control script on a single object such as the player, then use a ray cast to decide which object is receiving the command.
A simple example:
In the central input control's update function:
If key down 'o'
Raycast to center of screen.
If hit object is a control panel
Call the ToggleDoor() method on the control panel.
Reference: Ray casting to center of screen.
The key issue is that you have to make sure that only one control panel is responding to the key down.