Search Unity

Action Key

Discussion in 'Scripting' started by mattconley2011, Nov 3, 2010.

  1. mattconley2011

    mattconley2011

    Joined:
    Feb 22, 2010
    Posts:
    105
    I wanna have an action key, like a lot of games, such as hitting e will cause an animation to happen if you are with in 1 foot from an object

    Thanks!
     
  2. nalim

    nalim

    Joined:
    Apr 13, 2010
    Posts:
    118
    you cant just say i want this !
    you have to explain it better...
    and what do already have ?
    what kind of genre is your game ? (typ)
     
  3. mattconley2011

    mattconley2011

    Joined:
    Feb 22, 2010
    Posts:
    105
    function Update () {
    if (Input.GetButtonDown ("Action"))

    animation.Play();
    }

    What more is there to explain? genre really has nothing to do with this question, i could use the script i need for any game really. It's 3rdPS. And i've seen plenty of people say on here, what they are looking for and get multiple answers, without telling what genre their game is lol
     
  4. Chris-Sinclair

    Chris-Sinclair

    Joined:
    Jun 14, 2010
    Posts:
    1,326
    Looks like you answered your own question mattconley. Just script it in a way that makes sense for your game.
     
  5. FluidPixel_Gavin

    FluidPixel_Gavin

    Joined:
    Jan 6, 2010
    Posts:
    88
    lol. Yup you answered your own question. I think you might want to take a look at event systems, i think thats where youre going with the question.
     
  6. mattconley2011

    mattconley2011

    Joined:
    Feb 22, 2010
    Posts:
    105
    I don't know how to script it, i just know how it should be done lol Anyone know?
     
  7. FluidPixel_Gavin

    FluidPixel_Gavin

    Joined:
    Jan 6, 2010
    Posts:
    88
    The simplest form of it is essentially;

    if event
    ----->trigger(event)

    So some condition has been met, we must handle this event.

    You could create a singleton like object which manages these events or each object could manage it. There is many ways to get an event manager set up but its all very game specific so you would have to code it in a way that suits your game.
     
  8. tonyd

    tonyd

    Joined:
    Jun 2, 2009
    Posts:
    1,224
  9. FluidPixel_Gavin

    FluidPixel_Gavin

    Joined:
    Jan 6, 2010
    Posts:
    88
    I think i'll bookmark your thread for future posters tonyd :)
     
  10. mattconley2011

    mattconley2011

    Joined:
    Feb 22, 2010
    Posts:
    105
    Is there tutorial i can see to learn how to do it?
     
  11. Chris-Sinclair

    Chris-Sinclair

    Joined:
    Jun 14, 2010
    Posts:
    1,326
  12. FluidPixel_Gavin

    FluidPixel_Gavin

    Joined:
    Jan 6, 2010
    Posts:
    88
  13. mattconley2011

    mattconley2011

    Joined:
    Feb 22, 2010
    Posts:
    105
  14. Chris-Sinclair

    Chris-Sinclair

    Joined:
    Jun 14, 2010
    Posts:
    1,326
    What are you asking for? Tutorials on how to script in general or tutorials on how to create an "action button" for which we already explained that it's too general of a case and relatively simple to script?
     
  15. tonyd

    tonyd

    Joined:
    Jun 2, 2009
    Posts:
    1,224
    Someone to write his game for him, apparently.
     
  16. mattconley2011

    mattconley2011

    Joined:
    Feb 22, 2010
    Posts:
    105
    I'm not finding anything on this, i want the action key enabled only when so close of range of an object
     
  17. tonyd

    tonyd

    Joined:
    Jun 2, 2009
    Posts:
    1,224
    If you have even the slightest understanding of Unity, game logic, and scripting in general, this should be fairly simple to code.

    If you can't figure it out, you might want to consider a programming class or coding a few pong type games before attempting to create a full-blown shooter.
     
  18. Tomme

    Tomme

    Joined:
    Jun 17, 2010
    Posts:
    66
    This is a simple way to do it, will need work if you need a door opening or something, but will work.
    Sorry if this is written badly, done it quick and have never had good grammar and spelling. :)

    Remember Triggers they are excatly what you want, when said criteria are met then do stuff.

    Action Area script - Add a collider then make it a trigger
    actionArea.js
    This script should be attached to a game object which has a collider of sorts that has the "is Trigger" ticked. Drag your player into the "theUser" varible and when your player is in the collider area the script will comminucate with the script below telling it that the criteria are met allowing actions such as animations and more.
    Code (csharp):
    1.  
    2. var theUser : GameObject; //the player
    3. var actionStuff : actionScript; //the script contianing the input and animation stuff
    4.  
    5. function OnTriggerEnter (myTrigger : Collider)
    6. {
    7.     if(myTrigger.gameObject == theUser)
    8.     {
    9.         Debug.Log("You are in the action area");   
    10.         actionStuff.canAction = true;
    11.         Debug.Log(actionStuff.canAction);
    12.     }
    13. }
    14.  
    15. function OnTriggerExit (myTrigger : Collider)
    16. {
    17.     if(myTrigger.gameObject == theUser)
    18.     {
    19.         Debug.Log("You left the action area");
    20.         actionStuff.canAction = false;
    21.         Debug.Log(actionStuff.canAction);
    22.     }
    23. }
    24.  
    Animation Input Script
    actionScript.js
    The script above returns a boolean to this script and if this boolean equals true then when your press "Fire1" the actions you will want can be performed and wont be perfomed if the boolean equals false.
    Code (csharp):
    1.  
    2.  
    3. var canAction : boolean = false;
    4.  
    5. function Update ()
    6. {
    7.     if(canAction)
    8.     {
    9.         if(Input.GetButtonDown("Fire1"))
    10.         {
    11.             //Play animation, do stuff
    12.         }
    13.     }
    14. }
    15.  
    16.  
    May need proof reading, may of made errors as I have done it quick.

    Tomme
     
    Last edited: Nov 3, 2010
  19. mattconley2011

    mattconley2011

    Joined:
    Feb 22, 2010
    Posts:
    105
    not getting any errors, but i can't apply the action script, and it doesn't send the debug message when i enter the trigger