- Home /
how to make player open door?
Hi
How can I make a script that can make a door open when the character collides with it?
(I animated the door in unity)
Answer by MC HALO · Apr 08, 2011 at 05:55 PM
Ok this is what you need to do :
function OnControllerColliderHit(hit: ControllerCollider) {
if(hit.gameObject.tag == "Door"){ hit.animation.Play(" Put you Unity Animation name in here" );
} }
this will open the door if the player collides with it but in order for it t o work you need the door object to have the following tar attached to it "Door" then wait and see.
Example 2:
function OnCollisionEnter (hit: Collision){
if(hit.transform.tag == "Door"){
animation.Play("Put your Unity door animation name here ");
} }
Dont forget to tag the door object in unity if you dont know how to make tages do the following:
Click on the door gameObject and in the Inspector Mode look for "Tag"
if you find Tag click on it and a drop down menu will drop down from there select "Add tag"
and from there create a new tag called "Door"
then click on the door game object again and then click on tag and then add the tag that you created to the door
hope this helps :)
when I add "door" to tag it is appearing in the (Layer) list and not in the (Tag)
@$$anonymous$$ichael There is a little triangle next to the "tags" pop that open and add your tags there.
Yep OrangeLightning is right. click on the player on the Inspector mode click where it says tag and then select new tag. once you have done that you will see list of layers. On top of that you will see something called tags and next to that you will have a little triangle click on that and then a list of tags will fall down from there add your tag. here is a link:
http://unity3d.com/support/documentation/Components/class-Tag$$anonymous$$anager.html
or try this link for help:
http://unity3d.com/support/documentation/Components/Tags.html
Answer by Paige · Apr 08, 2011 at 05:54 PM
I haven't tried out animations yet, but if I were to guess from my knowledge so far, I would say just have on the character controller an on collider enter function with the animation starting when the player hits the door.
yup, figured it was something easy like that, I just didn't know the animation code yet to help him out. nice one