Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

how to make it so button can only be pressed once its inside a trigger

Discussion in 'Scripting' started by xxfmxx, Feb 27, 2015.

  1. xxfmxx

    xxfmxx

    Joined:
    May 28, 2014
    Posts:
    32
    So im having trouble trying to get the button to work only when it is inside the trigger object which in my case
    is a ladder. I know once some one provides the answer im going to feel like an idiot but my brain is being stupid and will not cooperate.

    basically i want the player object to only be able to press a button in this case u ... only if its in within the ladder collider trigger otherwise he cant press it.


    i have the sample project attached if anyone wants to look at it and see how i set the triggers and tags up...its file size isn't too big

    but here is the code if you don't its pretty basic which is why its killing me that i cant figure this out

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Ladder : MonoBehaviour {
    5.  
    6.     bool uPressed = false;
    7.     bool insideTrigger = false;
    8.     public bool goup = false;
    9.     private Move movement;
    10.  
    11.     //Use your existing collider code to set this value as applicable
    12.  
    13.     void awake ()
    14.     {
    15.         movement = GetComponent<Move>();
    16.     }
    17.  
    18.  
    19.    
    20.     void Update()
    21.     {
    22.  
    23.  
    24.         if (insideTrigger = true)
    25.         {
    26.             uPressed = true;
    27.  
    28.             if(uPressed = true)
    29.             {
    30.                 Debug.Log("button true");
    31.             //Needs to be GetKey :)
    32.             if (Input.GetKey(KeyCode.U))
    33.             {
    34.                 Debug.Log ("up up up up up");
    35.                 uPressed = true;
    36.                 float translation = Time.deltaTime * 2;
    37.                 transform.Translate(0, translation, 0);
    38.                
    39.             }
    40.             }
    41.             else
    42.             {
    43.                 uPressed = false;
    44.             }
    45.         }
    46.  
    47.         //if (goup)
    48.         ////{
    49.        
    50.         //}
    51.  
    52.         /*else if(goup = false)
    53.         {
    54.        
    55.                 Debug.Log ("up up up up up");
    56.                 uPressed = true;
    57.                 float translation = Time.deltaTime * 0;
    58.                 transform.Translate(0, translation, 0);
    59.  
    60.         }*/
    61.        
    62.  
    63.  
    64.  
    65.  
    66.     }
    67.  
    68.     void OnTriggerEnter2D( Collider2D Col )
    69.     {
    70.  
    71.        
    72.            
    73.  
    74.        
    75.         //Debug.Log ("this is here");  
    76.         if(Col.gameObject.tag == "Ladder")
    77.         {
    78.             //if (Input.GetKey(KeyCode.U))
    79.             //{
    80.             goup = false;
    81.  
    82.             insideTrigger = true;
    83.             Debug.Log(" is hitting trigger");
    84.        
    85.  
    86.              }
    87.        
    88.     }
    89.  
    90.    
    91.     void OnTriggerExit2D( Collider2D Col )
    92.     {
    93.         if(Col.gameObject.tag == "Ladder")
    94.         {
    95.             goup = false;
    96.             uPressed = false;
    97.             insideTrigger = false;
    98.             Debug.Log("Object is not hitting trigger");
    99.            
    100.         }
    101.     }
    102.     //void FixedUpdate()
    103.     //{
    104.         //if (uPressed)
    105.         //{
    106.             //Will add further force on top of existing, might not be want you want?
    107.             //GetComponent<Rigidbody2D>().AddForce(Vector3.up * Time.fixedDeltaTime);
    108.         //}
    109.     //}
    110.          
    111.        
    112.     }
    113.  
     

    Attached Files:

  2. Buddy_Redmond

    Buddy_Redmond

    Joined:
    Aug 30, 2014
    Posts:
    23
    Not sure if it is your problem, I could use some description of what IS happening not just what is supposed to happen, but lines 24 and 28 should probably have == instead of =. If insideTrigger is always true, that is probably why.
     
  3. xxfmxx

    xxfmxx

    Joined:
    May 28, 2014
    Posts:
    32
    thanks ill try that out but basically the player can press the button outside of the trigger object and it then proceeds to move up .. and i did attach the entire project with the thread so people can check out whats happening since the project is really really really small in file size
     
  4. xxfmxx

    xxfmxx

    Joined:
    May 28, 2014
    Posts:
    32
    just tried that out it totally fixed the problem :) you are awesome @Buddy_Redmond