- Home /
triggering a door to open with a button
im trying to figure out how to trigger my hoist door to open with the space bar. however i only want to be able to open the hoist door when im in the trigger box that i made. the script im using gives no errors but it still doesnt work. heres the script:
var doorUp : AnimationClip;
function OnTriggerEnter (mytrigger : Collider) {
if(mytrigger.gameObject.name == "player" )
{
if(Input.GetButton("openDoor"))
{
door = gameObject.Find("door");
door.animation.Play("hoistDoorUp");
}
}
}
Answer by Chris D · Jul 05, 2011 at 12:01 AM
The first thing that pops out at me is that you're using OnTriggerEnter
and not OnTriggerStay
.
The former will only fire for the frame it occurs on (better be holding down the openDoor
key) whereas the latter will check for the keypress as long as you're in the trigger area.
excellent thanks very much thats exactly what I needed!
and i have another question...i have a player with a character controller, but i need to make sure that some sort of gravity like system is applied to it. heres the script for my player. what should i add to apply gravity? script:
private var controller : CharacterController;
var speed : float = 4f;
public var woman : Transform;
function Start ()
{
controller = GetComponent(CharacterController);
}
function Update () {
moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0,
Input.GetAxis("Vertical"));
moveDirection = woman.TransformDirection(moveDirection);
moveDirection *= speed;
controller.$$anonymous$$ove(moveDirection * Time.deltaTime);
}
@script RequireComponent(CharacterController)
Two things:
If you're going to post code, please make sure it's formatted correctly (if you're posting to the comments section, it's easier to compose everything properly in the Answer box, then copy/paste over because formatting is a pain)
Don't post new questions in an older question. Take a $$anonymous$$ute to search through older questions (search box in the top right) and, if you can't find something that helps, open a new one. This question has been answered and the answer has been accepted so most people won't give it a second glance.
Your answer
Follow this Question
Related Questions
OnTriggerEnter gives me error? 2 Answers
Disable Mesh Collider Trigger 1 Answer
OnTriggerEnter fails to activate 1 Answer
Button back not clickable after rotation animation 1 Answer
Activating One Part of An Animation 2 Answers