- Home /
Sliding door animation question.
Hey, so, I'm currently doing a project for a class, and I'm having so much trouble going about it. What I'm trying to do is activating an animation of closing a door (slide from left to the right wall) and flashing lights when my first person controller triggers it by stepping on a game object that I've placed before the door.
Currently, I'm trying to use the function OnTriggerEnter, but I have no clue how to script that when my FP-controller steps onto the game object stationed before the door, it will cause the door to shut and the lights above to start flashing. Oppositely, I want to use the OnTriggerExit, so when my controller leaves the game object, the door opens and the lights stop flashing.
Can anyone help? I've asked a couple of friends, but all they did was just confuse me even more. Thank you for any future responses!
Answer by AlucardJay · Apr 15, 2012 at 04:58 AM
The answers on this question are probably just confusing you more , so I shall start from the beginning to help explain the process you need to follow.
I'll be using video links to help you see the concepts working.
Using Triggers and OnTriggerEnter :
Beginner B13 - Trigger Collision Detection
http://www.unity3dstudent.com/2010/07/beginner-b13-trigger-collision-detection/
Note how setting the trigger to True (tick), and setting the Mesh Renderer to False (Untick), you can create a 'zone' that you can place where you want the trigger to occur.
Using Find and SendMessage :
Beginner B28 - Send Message
http://www.unity3dstudent.com/2011/02/beginner-b28-sendmessage-to-call-external-functions/
The 'react' script for example would activate the animation (instead of changing the material) Then the other important part is the Find and SendMessage commands.
Unity animation intro :
Intermediate I02 – Basic Animation and Events
http://www.unity3dstudent.com/2010/09/intermediate-i02-basic-animation-and-events/
Overview on animation.
Also , and probably most Importantly , read the Unity Scripting Reference on the commands you want to use :
http://unity3d.com/support/documentation/ScriptReference/Collider.OnTriggerEnter.html
http://unity3d.com/support/documentation/ScriptReference/GameObject.Find.html
http://unity3d.com/support/documentation/ScriptReference/GameObject.SendMessage.html
Here is the link to other video's in the beginner set by Unity3Dstudent :
http://www.unity3dstudent.com/category/modules/beginner/
If you watch these and are still having trouble, I am happy to help you edit your scripts to get the effect you are after.
//
For an example , you could also watch these video's (this is just to show how someone else is using these concepts). Remember this is just an example , not a tutorial for you to follow. This is part of a much bigger project, and it is also in C# , not unity JavaScript. So just focus on the gameObject , the Trigger function, and the GameObject.Find commands.
http://www.3dbuzz.com/vbforum/content.php?225-Unity-3rd-Person-Platformer-Game
Scroll down to Section 2: Code Development
Download and watch : 5 - ButtonPush , and , 6 - Temple Door .
Do what this man says. $$anonymous$$uch more thorough info than I supplied.
Sorry it took me so long to reply! I did look through all this info, and it totally helped! I did it! Anyways... ahem Thank you so much! All of you!
Answer by JayMHelpsU · Apr 14, 2012 at 07:12 PM
Well the door would be just a matter of creating an animation but the lights im not too sure on doing, are they just Directional Lights?
The code for the animation is gameObject.animation.Play("animation name here");
I actually chose to use point lights because I wanted to the lights to rotate as well (so they would spin in circles).
As for the animations, I had those already created, but I'm just not sure how I can trigger them via stepping on the game object with the function OnTriggerEnter and OnTriggerExit scripts.
You can use a trigger on the floor and docking the code with OnTriggerEnter to that object on the floor :)
Could you elaborate xD? Would the game object be that trigger?
Ok, say you made an invisible object on the ground as a trigger, so when the player walks on it, it activates OnTriggerEnter. In order for that to activate you need to make a code and dock the code to that object on the floor. I hope this helped in the trigger part :/
Answer by yezzer · Apr 14, 2012 at 07:24 PM
Where are you stuck? Start simple. Attach a script with ontriggerenter and ontriggerexit to your trigger gameobject, and get it to Debug.Log when these are called.
After this, in your trigger script, add references to your door gameobject and light gameobject. Set them to active true/false when the controller enters/exits.
Then build upon that :)
Did that help? Though I could write it all for you, you won't learn anything ;)
Also look at iTween for animating the door and light :) you could get some nice, simple effects with that.
Ahhh yet again yezzer brilliant idea, why didnt i think of iTween i use it all the time DAHH :)
Thank you for that explanation yezzer! What I have right now is this:
// Destroy everything that enters the trigger
function OnTriggerEnter (Player : Collider) { if(Player.gameObject == "Prisoner") gameObject.Send$$anonymous$$essage }
As you can see, this script is still very unfinished xD! So, I think I have the OnTriggerEnter function correct, but when I start writing within the brackets, that is where I'm stuck. When I was at school and talked to my friend, he said something about using a send message script. I looked it up on the Unity Script reference, but it ended up confusing me even more (Sorry for my very little understanding about all of this. This is my first year scripting)!
You've helped a lot already, but could you briefly explain the true/false part? I remember hearing our $$anonymous$$cher talking about it, but it's still an area where I'm still unsure about.
function OnTriggerEnter (Player : Collider) { if(Player.gameObject == "Prisoner"){ Debug.Log("prisoner entered trigger"); } } Attach that script to the trigger object, open the console, press play, move the gameobject with the name Prisoner into the trigger. Check the console for that message. Is that working?