Search Unity

How to create a WASD control in unity3d?

Discussion in 'Immediate Mode GUI (IMGUI)' started by sotongqueen, Jan 22, 2009.

Thread Status:
Not open for further replies.
  1. sotongqueen

    sotongqueen

    Joined:
    Dec 15, 2008
    Posts:
    59
    Does anybody know how can i use the following code :

    Code (csharp):
    1.  
    2. var speed = 5.0;
    3. function Update () {
    4. var x = Input.GetAxis("Horizontal") * Time.deltaTime * speed;
    5. var z = Input.GetAxis("Vertical") * Time.deltaTime * speed;
    6. transform.Translate(x, 0, z);
    7. }
    8.  
    to create a WASD control in unity3d? Which mean there will be 4 button, W, A, S, D in my scene so that whenever my user press on any of the 4 buttons, eg,the W button, it will move it to the left? thanks in advance.
     
  2. HiggyB

    HiggyB

    Unity Product Evangelist

    Joined:
    Dec 8, 2006
    Posts:
    6,183
    Look at the first person controller prefab that comes with Unity (Standard Assets/Prefabs) and the scripts it uses as you'll see a working example of this there already.


    Oh, and this has nothing at all to do with Unity GUI, moving to scripting in 3... 2..1 1...
     
  3. sotongqueen

    sotongqueen

    Joined:
    Dec 15, 2008
    Posts:
    59
    Well, i am sorry i don't think this is what i want. What i want is that i will need to create some control function for my user to control the interface once it click on the debug button.

    Which means when i click on the debug button, my button "W" "A" "S" "D" will be automatically appear in the interface. (which i am sure i will need to use GUI function.) So that instead of using the keyboard key to control around my interface, i am able to control it by pressing on this 4 available buttons after debugging.

    Thanks
     
  4. HiggyB

    HiggyB

    Unity Product Evangelist

    Joined:
    Dec 8, 2006
    Posts:
    6,183
    Just make an OnGUI function that displays four buttons (using GUI.Button()), one for each of the characters of interest (W, A, S, D). When pressing each of those buttons you simply issue the translate command. Here is an example for one such button, I'll leave it to you to implement the other three and arrange your GUI buttons as you see fit.


    Code (csharp):
    1. // Untested forum code
    2. function OnGUI () {
    3.  
    4.   if (GUI.Button(Rect(10,10,20,20), "W")) {
    5.     var tZ = Time.deltaTime * speed;
    6.     transform.Translate(0,0,tZ);
    7.   }
    8.  
    9. }
    The above example assumes that you add the OnGUI function to the same script that contains your Update function above (thus they share the same speed variable).


    And I'll move this back to the GUI section now that it's clear you were in fact after some GUI help.
     
  5. sotongqueen

    sotongqueen

    Joined:
    Dec 15, 2008
    Posts:
    59
    Thanks Higgyb and i got this. Really thanks a lot. However, i got a few things i would like to ask, i hope you don't mind.

    First, is it possible to increase the font size of the text displaying in the GUI.Label?

    Secondly, is there a method to disable this 4 "WASD" button when i click on something on the keyboard?

    For example, when i press 1 on the keyboard, my WASD button on my interface will be disappear? However, when i click on 2 my WASD button on my interface will reappear again?

    Thanks in advance.
     
  6. HiggyB

    HiggyB

    Unity Product Evangelist

    Joined:
    Dec 8, 2006
    Posts:
    6,183
    Not using the default GUI skin as-is. You'll need to import the font you want to use (must be TrueType) and use the Import Settings to control the display size. Then use that font as part of a custom GUI skin or style when desired.


    Sure. Have some variable (a boolean) that controls whether those buttons are active. When the user presses whatever key you want to use as an on/off switch you toggle that boolean value. Modify the buttons so they're only displayed if that boolean is true.

    Code (csharp):
    1. // Untested forum code
    2. var ButtonsActive = true;
    3.  
    4. function OnGUI () {
    5.   if (ButtonsActive) {
    6.     if (GUI.Button(Rect(10,10,20,20), "W")) {
    7.       var tZ = Time.deltaTime * speed;
    8.       transform.Translate(0,0,tZ);
    9.     }
    10.   }
    11. }
    12.  
    13. function Update () {
    14.   if (Input.GetKeyDown("1")) {
    15.     ButtonsActive = true;
    16.   } else if (Input.GetKeyDown("2")) {
    17.     ButtonsActive = false;
    18.   }
    19. }
    Something like that should work.
     
  7. sotongqueen

    sotongqueen

    Joined:
    Dec 15, 2008
    Posts:
    59
    Thanks for the code. i will try it out later. However, i don't understand what do you mean by "using the import setting to control the display size?"

    Thanks in advance. [/quote]
     
  8. pete

    pete

    Joined:
    Jul 21, 2005
    Posts:
    1,647
    think the primary mention is here (might be wrong):
    http://unity3d.com/support/documentation/Manual/Meshes.html

    it applies to any importable item though (should be better explained in the docs Tom fwiw). basically select it in the project view and right/ctrl click it and select import settings. that'll pop up a window where you can change import settings. you can also get to it through the assets menu.
     
  9. HiggyB

    HiggyB

    Unity Product Evangelist

    Joined:
    Dec 8, 2006
    Posts:
    6,183
    Please consult the docs, specifically this page:

    Fonts

    Note that the above is found by going to the docs, then the component reference, then near the top under Asset Components you'll find a link for the Fonts page above.


    That page is not appropriate for fonts at all as a font specific import settings dialog is used for font assets as per the doc page I linked to above.
     
  10. pete

    pete

    Joined:
    Jul 21, 2005
    Posts:
    1,647
    ah i didn't realize that was there. been a while since i really looked at the manual. import settings still applies and "general usage of" is what i meant by the better explained comment. i don't remember it mentioned as part of general asset handling. you use it pretty much on every asset. so a general statement on it might be appropriate there. that's all i meant. if it is there, nm! don't quite recall. thx for the better link! <ot again>
     
  11. sotongqueen

    sotongqueen

    Joined:
    Dec 15, 2008
    Posts:
    59
    Hi, i would like to check is there a way to place the position of the "W"ASD button fixed on the screen? What i mean is that i wanted my WASD button to be positioning in the middle of my interface fixed so that even i have to change any of the resolution setting, the button will still remain at the same positon that i place as it is.

    As, i realize whenever i change my resolution to 1024 * 768, the button of my WASD position seem disappear. i can only view the control button back if only i choose my default screen resolution to 1920 * 1200.

    Please kindly help. Thanks in advance.
     
  12. Quietus2

    Quietus2

    Joined:
    Mar 28, 2008
    Posts:
    2,058
  13. sotongqueen

    sotongqueen

    Joined:
    Dec 15, 2008
    Posts:
    59
    Thanks for the reply quietus. Hmm.. do you mind show an example of how can script it in coding using unity3d?

    As i really have no idea how can i apply the Screen.height in my coding.

    Example of my coding for my control button in my screen is this:

    Code (csharp):
    1.  
    2.  
    3. function OnGUI () {
    4. var speed = 15.0;
    5.  
    6.    if( GUI.Button(Rect(1400,505,40,40),"S")){
    7.     var tS = Time.deltaTime * speed;
    8.     transform.Translate(0,tS,0);  
    9.    
    10.    }
    11.    
    12.    if (GUI.Button(Rect(1400,460,40,40), "W")) {
    13.    
    14.     var tW = Time.deltaTime * speed;
    15.     transform.Translate(0,0,tW);
    16.   }
    17.  
    18.   if (GUI.Button(Rect(1350,505,40,40), "A")) {
    19.     var tA = Time.deltaTime * speed;
    20.     transform.Translate(-tA,0,0);
    21.   }
    22.  
    23.  
    24.   if (GUI.Button(Rect(1450,505,40,40), "D")) {
    25.     var tD = Time.deltaTime * speed;
    26.     transform.Translate(tD,0,0);
    27.      }
    28.  
    29. }
    30.  
    31.  
    however, i not sure how can i script in a way such that this few control button will still appear in the same positon even after i choose a lower resolution such as 1024 * 768 instead of 1920 * 1200. Thanks in advance. [/code]
     
  14. flaminghairball

    flaminghairball

    Joined:
    Jun 12, 2008
    Posts:
    868
    This will place one button in the center of the screen:

    Code (csharp):
    1. function OnGUI() {
    2.      if(GUI.Button(Rect(Screen.width / 2, Screen.height / 2, 64, 22), "W"){
    3.           //Translate
    4.      }
    5. }
     
  15. sotongqueen

    sotongqueen

    Joined:
    Dec 15, 2008
    Posts:
    59
    Thanks. However, i wanted my control button to be fixed at all time. I realize this script does not work for me. As When i manually position the control in a resolution of 1920*1200. However, when i reduce my resolution to 1024*768. My WASD control are not back to the original position. What i want is i need my control button to be at fixed position at all time(See attached picture) even after i reduce my resolution to 1024*768 or even lower. May i achieve that? If yes, would someone show me some example on that? Thanks
     

    Attached Files:

  16. Quietus2

    Quietus2

    Joined:
    Mar 28, 2008
    Posts:
    2,058
    You screenshot shows a position relative to the current resolution, not a fixed position. If it were fixed, when you lowered your resolution your buttons would move off to the right and bottom of the screen.

    The solutions we offered as presented are a good starting point for what you are showing in your screenshots. For, when figuring out a relative position offset it's important to know relative to -what-. That's where the screen center comes into play. From that point on, it's simply arithmetic.
     
  17. sotongqueen

    sotongqueen

    Joined:
    Dec 15, 2008
    Posts:
    59
    Yap that the problems i am facing when i lowered my resolutions, my buttons seem moving to the right and botton of the screen. I want my button to stay neatly in such a way just like the screenshot shown.

    i tried the following coding but still when i lowered my resolution, my buttons still move to the right.

    Code (csharp):
    1.  
    2. function OnGUI () {
    3. var speed = 15.0;
    4.  
    5.  
    6.    if (GUI.Button(Rect(500,Screen.height-40,40,40), "S")) {
    7.     var tS = Time.deltaTime * speed;
    8.     transform.Translate(0,tS,0);
    9.   }
    10.  
    11.    if (GUI.Button(Rect(500,Screen.height-82,40,40), "W")) {
    12.    
    13.     var tW = Time.deltaTime * speed;
    14.     transform.Translate(0,0,tW);
    15.   }
    16.  
    17.   if (GUI.Button(Rect(455,Screen.height-40,40,40), "A")) {
    18.     var tA = Time.deltaTime * speed;
    19.     transform.Translate(-tA,0,0);
    20.   }
    21.  
    22.  
    23.  if (GUI.Button(Rect(550,Screen.height-40,40,40), "D")){
    24.     var tD = Time.deltaTime * speed;
    25.     transform.Translate(tD,0,0);
    26.      
    27.       }
    28.    
    29.    }
    30.  
    31.  
    32.  
    33.  
    i really lost. Thanks in advance.
     
  18. Quietus2

    Quietus2

    Joined:
    Mar 28, 2008
    Posts:
    2,058
    Code (csharp):
    1. screen.height - 40
    That is a fixed offset that does not scale with your screen resolution. As I said previously, it's basic elementary school arithmetic. You need to calculate a proportion based upon the current resolution. In order to calculate a proportion you need to divide, and I sure don't see any division going on!

    The only thing to keep in mind, is that the scale factor of your proportion is only going to valid for a particular aspect ratio. Especially these days, not everyone is running at 4:3.
     
  19. sotongqueen

    sotongqueen

    Joined:
    Dec 15, 2008
    Posts:
    59
    Thanks. This post have been solved! =DD
     
  20. shan_123

    shan_123

    Joined:
    Dec 18, 2015
    Posts:
    1
    What is the c sharp code for WASD control buttons in roll a ball tutorial?
     
Thread Status:
Not open for further replies.