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

If(Input.GetKeyDown("f")) Does not work on

Discussion in 'Scripting' started by Csharpner, Feb 9, 2015.

  1. Csharpner

    Csharpner

    Joined:
    May 27, 2013
    Posts:
    7
    So i have a fireworks rocket that has a rigid body, and when i enter its collider that has a trigger and the parent of the collider has the rigidbody, i want to be able to press "F" to set the y-axis upwards.

    Here it is:

    1. #pragma strict

    2. var Firework : Rigidbody;

    3. function OnTriggerEnter(Other : Collider)
    4. {
    5. Debug.Log("BOOM!"); // This gets called
    6. if(Other.gameObject.tag == "Player")
    7. {
    8. Debug.Log("BOOM!"); // This also gets called
    9. if(Input.GetKeyDown("f"))

    10. {
    11. Debug.Log("BOOM!"); // This does not get called. So the problem is after "if(Input.GetKeyDown("f"))"
    12. fire();
    13. }
    14. }
    15. }

    16. function fire () {
    17. rigidbody.velocity = Vector3(0,150,0);
    18. }

    Thanks for reading :)
     
    Last edited: Feb 9, 2015
  2. pSeth

    pSeth

    Joined:
    Mar 11, 2014
    Posts:
    10
    Well firstly I am pretty sure you use if(input.GetKey (KeyCode.F)).... Is there anyway you could write your code a little bit neater? I could barely read it
     
  3. DanielQuick

    DanielQuick

    Joined:
    Dec 31, 2010
    Posts:
    3,137
    Please use code tags when posting code.

    I assume you want to use OnTriggerStay, otherwise there will only be a single frame when you enter the trigger that you have a chance to press the key.
     
    Ramsdal likes this.
  4. Ramsdal

    Ramsdal

    Joined:
    Oct 18, 2013
    Posts:
    251
  5. The-Little-Guy

    The-Little-Guy

    Joined:
    Aug 1, 2012
    Posts:
    297
    You probably want to add it to the update function, and in the trigger function, set a boolean or something that gets checked in the update.

    so for example:

    Code (CSharp):
    1. var triggered : bool;
    2.  
    3. function OnTriggerEnter(){
    4.     triggered = true;
    5. }
    6.  
    7. function Update(){
    8.     if(triggered){
    9.         if(Input.GetKeyDown("f")){
    10.              // Do my event here
    11.         }
    12.         triggered = false;
    13.     }
    14. }
     
    Last edited: Feb 9, 2015
  6. Csharpner

    Csharpner

    Joined:
    May 27, 2013
    Posts:
    7
    haha, i know lol:D. Its one of my first scripts though :p
     
  7. Csharpner

    Csharpner

    Joined:
    May 27, 2013
    Posts:
    7
    Nothing is happening i have the collider set to trigger, checked the size of the collider, and everything.

    1. var triggered : bool; did not work, so i changed it too
      1. var triggered : boolean;
     
  8. Csharpner

    Csharpner

    Joined:
    May 27, 2013
    Posts:
    7
    Done now, thanks Daniel and Ramsdal :D