- Home /
Button Box Door Collision
How do I type a code which is applied to the button (Cylinder shape with a collider)which states if the GameObjects (I want two or more different things) collides with the button (itself), it says that a GameObject (Door) plays animation 1(Opening) and then if the Door senses no collision, it plays animation 2(Closing). I want it to be easy and no Fixed gameobjects e.g. Gameobject.Tag=="Something". I hope this is making sense. I'm learning Javascript. Thanks.
I have the colliders now on, no more errors but when I attach the script to the button, it won't let me select the animation for open/close door. The 'DoorFlat' has the animation on it.
The Script for the button slightly modified:
var opendoor: Animation;
var closedoor: Animation;
function OnCollisionEnter(collision : Collision) {
if (collision.gameObject.tag=="Player")
{
opendoor.Play("opendoor");
}
}
function OnCollisionExit(collision : Collision) {
if (collision.gameObject.tag=="Player")
{
closedoor.Play("closedoor");
}
}
sorry . it shoult be AnimationClip;
and
should be
animation.Play(opendoor);
ins$$anonymous$$d of
opendoor.Play("opendoor");
Also you could read comment on my answer at the bottom.
Answer by Lordinarius · Feb 02, 2013 at 08:47 AM
You are tring to make a physical button in game. Am i right ? if it is yes
var opendoor: Animation;
var closedoor: Animation;
function OnCollisionEnter(collision : Collision) {
if (collision.gameObject.tag=="myTag")
{
opendoor.Play();
}
}
function OnCollisionExit(collision : Collision) {
if (collision.gameObject.tag=="myTag")
{
closedoor.Play();
}
}
You dont need var opendoor : Animation or closedoor : Animation.
Whatever animation is attached to the game object, just assign that game object:
var doorObject : GameObject;
animationName.animation.Play();
Answer by Lemo76 · Feb 09, 2013 at 10:26 PM
Sorry but I'm not seeming to get it correct! Here's the script applied to the button:
var opendoor: Animation; var closedoor: Animation;
function OnCollisionEnter(collision : Collision) {
if (collision.gameObject.tag=="Player")
{
animation.Play("opendoor");
}
}
function OnCollisionExit(collision : Collision) {
if (collision.gameObject.tag=="Player")
{
animation.Play("closedoor");
}
}
Here is a picture to show the door and the button.