Results 1 to 5 of 5
  1. #1
    Join Date
    Apr 2005
    Location
    Ich lebe in den USA
    Posts
    2,486

    Quick Trigger question plz help

    So I want to do something really simple, but I don't have the know-how.


    I have a door that I made in Max, and I imported it into Unity. All I want, is for the door to open when the player gets close, and then close when the player moves away. I also want the door to play an opening and a closing sound with its animation.

    I know how to animate the door, but I am having a hard time getting it to open based on the player's proximity.

    Little help plz?

    Thnx

  2. #2
    Join Date
    Apr 2012
    Posts
    6
    I don't know alot, so take this for what it's worth.

    It seems to me that you could use OnTriggerEnter. From my studies you need to have at minimum a collider and a rigidbody. So, set up a plane before your door with a collider attached, either in the form of a vertical plane or sort of like a place mat. As soon as the player (with a rigidbody) "steps" on or "enters" the plane, do something. In your case, animate the door.

    If I remember correctly, the AngryBot demo uses something close to this.

  3. #3
    Join Date
    Apr 2005
    Location
    Ich lebe in den USA
    Posts
    2,486
    couldn't I just have the player controller collide with the gate itself?

    So I'm going for a script, here is what I have so far...


    Code:
    var sound : AudioClip;
    var Gate : GameObject;
    
    function OnTriggerEnter (other : Collider)
    {
    
    var contact : String = other.transform.name;
    
    if (contact == "FirstPersonController")
    {
    Gate.animation["Take 0"].time = 0;;
    Gate.animation["Take 0"].speed = 1;
    Gate.animation.Play("Take 0");
    // Play sound
    if (sound)
    AudioSource.PlayClipAtPoint(sound, transform.position);
    
    yield WaitForSeconds(5.0); // wait for 5 seconds
    // play animation in reverse
    animation["Take 0"].speed = -1.0;
    // Rewind the Door animation to the start
    animation.Rewind("Take 0");
    }
    }

    Needless to say, my script isn't working, and I've no idea why.

  4. #4
    Join Date
    Apr 2005
    Location
    Ich lebe in den USA
    Posts
    2,486
    Okay now I've got a new setup. I think this one mostly works.


    Basically i have this invisible box for a game object. and my Gate is instanced inside of the Game Object.

    I have two scripts, one for the gate and another for the Game Object. (I found them online, copy / paste)

    The Game Object script, is supposed to activate the Gate script, and then the gate opens.


    Unfortunately, the Game Object script was made for another game, and it isn't triggered on Player Enter. So that's what I'm trying to figure out how to do. I need to know how to have the Game Object enable a trigger, on Player Enter.

    here's my code for the Game Object
    Code:
    using UnityEngine;
    using System.Collections;
    
    public class Activatable : MonoBehaviour {
    
        public GateFunction Gate;
        bool triggered = false;
        bool mouseOver = false;
        public bool Trigger {
           get{return triggered;}
           set{triggered = value;}
        }
    
        void Update()
        {
           if(triggered && mouseOver) //My triggered was sent from a button press of F in another script
             sendTrigger();         //It just sets Trigger = true;
        }
    
        void OnMouseEnter()
        {
           mouseOver = true;
        }
    
        void sendTrigger()
        {
           Gate.Trigger = true;
           triggered = false;
        }
    }
    And here's the code for the gate...
    Code:
    using UnityEngine;
    using System.Collections;
    
    public class GateFunction : MonoBehaviour {
    
        public enum GateState { open, closed, idle};
        GateState currentState = GateState.idle;
    
        public bool eventTriggered = false;
        public bool Trigger {
           get{return eventTriggered;} 
           set{eventTriggered = value;}
        }
    
        void Start()
        {
            StartCoroutine("CoStart");
        }
    
        IEnumerator CoStart()
        {
            while (true)
                yield return StartCoroutine("CoUpdate");
        }
    
        IEnumerator CoUpdate()
        {
               yield return StartCoroutine("changeStates");
        }
    
        IEnumerator changeStates()
        {
           if(eventTriggered)
           {
             if(currentState == GateState.idle)
             {
              currentState = GateState.open;
             }
            else if(currentState == GateState.open)
            {
             currentState = GateState.closed;
            }
          		else
              currentState = GateState.open;
    
             switch(currentState)
             {
              case GateState.idle :
                  animation.CrossFade("idle");
                  break;
              case GateState.closed :
                  animation.CrossFade("GateClose");
                  yield return new WaitForSeconds(animation.GetClip("GateClose").length);
                  animation.CrossFade("idle");
                  break;
              case GateState.open :
                  animation.CrossFade("GateOpen");
                  yield return new WaitForSeconds(animation.GetClip("GateOpen").length);
                  break;
             }
           }
           eventTriggered = false;
           yield return null;
        }
    }

  5. #5
    Join Date
    Mar 2011
    Posts
    3
    I think it is better to use a trigger.






    Code:
    public class DoorOpen2 : MonoBehaviour
    
    {
    	public AudioClip dooropen;
            public AudioClip door close;
    
    	void  OnTriggerEnter ( Collider other  )
    	{
    		if (other.gameObject.tag == "Player")
    		
    		{
    			GameObject Techdoor = GameObject.Find("TechDoor2");
    			Techdoor.animation.Play("TechDoor2Open");
    			audio.PlayOneShot(dooropen);
    		}
    	}
    	
    	void OnTriggerExit ( Collider other  )
    	
    	{
    		if (other.gameObject.tag == "Player")
    		
    		{
    			GameObject Techdoor = GameObject.Find("TechDoor2");
    			Techdoor.animation.Play("TechDoor2Close");
    			audio.PlayOneShot(doorclose);
    		}
    	}
    
    }

    In this instance TechDoor2 is the Door game object and the 2 animations are TechDoor2Open and TechDoor2Close.
    Apply this code to a trigger which is situated half way in front and half way behind the door so that as you approach it the door opens and you can pass through it, keep walking and the door will close.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •