Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice
  3. Dismiss Notice

How to use OnTrigger functions?

Discussion in 'Scripting' started by sam268, May 1, 2015.

  1. sam268

    sam268

    Joined:
    Apr 21, 2014
    Posts:
    149
    ok, so I am creating a script. There are actually two scripts, one goes on the door, and another goes on the "player manager" which is a super long thin ontrigger capsule collider that has a rigidbody on it. It basically collides with whatever the player is looking at. This is so that the player can interact with objects and doors and such. Another script is on the doorway, and it basically just says which scene the doorway leads to. Here is my player manager script so far.

    Code (csharp):
    1.  #pragma strict
    2.  
    3. private var isObject : boolean = false;
    4. private var isDoor : boolean = false;
    5. private var pausebutton : boolean = true;
    6. var doorScene : int;
    7. var GUI1 : GUISkin;
    8. var GUI2 : GUISkin;
    9.  
    10. function OnTriggerEnter (other : Collider) {
    11. if (other.gameObject.tag == "object") {
    12. isObject = true;
    13. }
    14. if (other.gameObject.tag == "door") {
    15. doorScene = other.gameObject.GetComponent.<Doorway>().nextScene;
    16. isDoor = true;
    17. }
    18. }
    19.  
    20. function OnTriggerExit (other : Collider) {
    21. if (other.gameObject.tag == "object") {
    22. isObject = false;
    23. }
    24. if (other.gameObject.tag == "door") {
    25. isDoor = false;
    26. }
    27. }
    28.  
    29.  
    30. function OnGUI () {
    31. GUI.skin = GUI1;
    32. if (pausebutton) {
    33.   if (GUI.Button(Rect(Screen.width/12 - (Screen.width/10)/2, Screen.height/12 - (Screen.height/10)/2, Screen.width/10, Screen.height/7), "")) {
    34.   Time.timeScale = 0;
    35.   pausebutton = false;
    36.   }
    37. }
    38.  
    39. //inventory basic GUI
    40. if (pausebutton == false) {
    41. GUI.Box(Rect(Screen.width/10.5 - (Screen.width/10)/2, Screen.height/12 - (Screen.height/10)/2, Screen.width/1.1, Screen.height/1.1), "");
    42. //backbutton
    43. GUI.skin = GUI2;
    44.   if(GUI.Button(Rect(Screen.width/8 - (Screen.width/10)/2, Screen.height/8 - (Screen.height/10)/2, Screen.width/10, Screen.height/7), "")) {
    45.   pausebutton = true;
    46.   Time.timeScale = 1;
    47.   }
    48. }
    49.  
    50. //pick up object
    51. if (isObject == true) {
    52.   if (GUI.Button(Rect(Screen.width/1.2 - (Screen.width/10)/2, Screen.height/2 - (Screen.height/10)/2, Screen.width/10, Screen.height/10), "")) {
    53.   TakeObject();
    54.   }
    55. }
    56.  
    57. //create open door button
    58. if (isDoor == true) {
    59.   if (GUI.Button(Rect(Screen.width/1.2 - (Screen.width/10)/2, Screen.height/2 - (Screen.height/10)/2, Screen.width/10, Screen.height/10), "")) {
    60.   OpenDoor();
    61.   }
    62. }
    63. }
    64.  
    65.  
    66. function TakeObject () {
    67. //pick up object and add it to inventory
    68. }
    69.  
    70. function OpenDoor () {
    71. //find the scene that the door is connected to, and change to that scene
    72. Application.LoadLevel(doorScene);
    73. }
    74.  
    However it doesn't work. When the player manager STARTS the scene touching the door, than the button appears for a brief few seconds before disappearing. If the player starts far away from the door, than moves towards the door, the button does not appear. What is going on?
     
  2. sam268

    sam268

    Joined:
    Apr 21, 2014
    Posts:
    149
    Ahhh, figured it out. My rigidbody on the player manager didn't have "is kinematic" checked. That fixed all my issues.