Search Unity

Using GUI for movement

Discussion in 'Scripting' started by Budgieboy, Sep 27, 2012.

  1. Budgieboy

    Budgieboy

    Joined:
    Sep 6, 2012
    Posts:
    40
    I want to create some on screen arrows using GUITexture so I can control my object on android(Since using Acceleration wasn't suitable) but I can't figure it out, any help?

    Code (csharp):
    1. if(Input.acceleration.y >0) //Checking for right arrow key
    2.        {
    3.          rightThruster.enableEmission = false;
    4.          leftThruster.enableEmission = true;
    5.          tilt += Vector3.down *0.05;
    6.        }
    7.        if(Input.acceleration.y <0) //Checking for left arrow key
    8.        {
    9.          leftThruster.enableEmission = false;
    10.          rightThruster.enableEmission = true;
    11.          tilt += Vector3.up *0.05;
    12.        }
    13.        if(Input.acceleration ==0) //Checking if no horizontal keys down
    14.        {
    15.          rightThruster.enableEmission = false;
    16.          leftThruster.enableEmission = false;
    17.        }
    18.        if(Input.acceleration.x >0) //Checking for up arrow key
    19.        {
    20.          topThruster.enableEmission = false;
    21.          bottomThruster.enableEmission = true;
    22.          speed += Vector3.forward *0.01;
    23.        }
    24.        if(Input.acceleration.x <0) //Checking for down arrow key
    25.        {
    26.          bottomThruster.enableEmission = false;
    27.          topThruster.enableEmission = true;
    28.          speed += Vector3.back *0.01;
     
  2. Karmarama

    Karmarama

    Joined:
    Jul 11, 2011
    Posts:
    261
    Shouldn't you be checking if your GUI Texture, or quad w/ texture (with raycast) is being "touched" first - then, if touched equals "leftArrow" then move your object with object.transform.Translate (or however you wish to move your object) in whichever direction you desire?
     
  3. Budgieboy

    Budgieboy

    Joined:
    Sep 6, 2012
    Posts:
    40
    That's what I was thinking at one point but wasn't sure how to do that or if it would even work so I just came here instead.

    I'm not at all good at coding.
     
  4. Karmarama

    Karmarama

    Joined:
    Jul 11, 2011
    Posts:
    261
    http://answers.unity3d.com/questions/199086/scripting-a-touch-on-an-object-in-ios.html

    Change your GUI Textures to planes (exported from a 3D app as one quad, 2 tris - the ones in unity have like..200 tris, which is unreasonable) and then apply your texture to each plane piece (atlas the texture, ie: place all arrows into one quad or rectangle texture (power of 2: 128x128 || 256x128) ) then UV your plane pieces to the arrows correctly (or mess with offset x y in unity's material/shader properties). Have the planes parented to the camera or some-such. Tag each plane (don't do a object name check) as either "leftArrow" or "fwdArrow" etc, and when you touch an individual plane piece to then run particular code - the URL I provided above should show you what you need.

    The reason your current code doesn't work is because your device has no idea that you're pressing the arrows - it's not detecting the press, and those arrows currently aren't set up to do anything even if it were pressed. Nor is it valid (AFAIK) to check "input.acceleration" with this. It can't automatically detect the pixels of your onGUI Texture to be an arrow, and correspond it's action accordingly to a function such as acceleration - that's bizarre.
     
    Last edited: Sep 27, 2012
  5. Budgieboy

    Budgieboy

    Joined:
    Sep 6, 2012
    Posts:
    40
    Thanks for the help, I've got the buttons to work but they only make the sounds on the object play and don't move it.

    I'm using planes like you said which obviously worked!

    I moved lines 5-10 from above the bracket with if(haveFuel) to below to try and solve the problem to no avail.

    Code (csharp):
    1. function Update ()
    2. {
    3.     if(haveFuel)
    4.         {
    5.             for (var touch : Touch in Input.touches){
    6.             if (touch.phase == TouchPhase.Began){
    7.             var userRay = Camera.main.ScreenPointToRay(touch.position);
    8.             var info : RaycastHit;
    9.             if(Physics.Raycast(userRay, info, 100.0f))
    10.             if(info.transform.CompareTag("rightbutton")) //Checking for right arrow key
    11.             {
    12.                 rightThruster.enableEmission = false;
    13.                 leftThruster.enableEmission = true;
    14.                 tilt += Vector3.down *0.05;
    15.             }
    16.             if(info.transform.CompareTag("leftbutton")) //Checking for left arrow key
    17.             {
    18.                 leftThruster.enableEmission = false;
    19.                 rightThruster.enableEmission = true;
    20.                 tilt += Vector3.up *0.05;
    21.             }
    22.             if(info.transform.CompareTag ==0) //Checking if no horizontal keys down
    23.             {
    24.                 rightThruster.enableEmission = false;
    25.                 leftThruster.enableEmission = false;
    26.             }
    27.             if(info.transform.CompareTag("upbutton")) //Checking for up arrow key
    28.             {
    29.                 topThruster.enableEmission = false;
    30.                 bottomThruster.enableEmission = true;
    31.                 speed += Vector3.forward *0.01;
    32.             }
    33.             if(info.transform.CompareTag("downbutton")) //Checking for down arrow key
    34.             {
    35.                 bottomThruster.enableEmission = false;
    36.                 topThruster.enableEmission = true;
    37.                 speed += Vector3.back *0.01;
    38.             }
    39.             speed *= 0.95f;
    40.             transform.Translate(speed, Space.Self);
    41.             transform.Rotate(tilt, Space.Self);
    42.             if(Input.GetAxis("Vertical")==0) //Checking if no vertical keys down
    43.             {
    44.                 bottomThruster.enableEmission = false;
    45.                 topThruster.enableEmission = false;
    46.             }
    47.             if(info.transform.CompareTag!=0)
    48.             {
    49.                 if(!audio.isPlaying)
    50.                 {
    51.                     audio.Play();
    52.                 }
    53.                
    54.                 fuelAmount-=(fuelBurnSpeed*Time.deltaTime); //check if fuel is being used.
    55.                 if(fuelAmount<=0) //if we ran out of fuel
    56.                 {
    57.                     fuelAmount=0;
    58.                     haveFuel=false;
    59.                     audio.Stop();
    60.                     bottomThruster.enableEmission=false; //disable thruster particles
    61.                     topThruster.enableEmission=false;
    62.                     leftThruster.enableEmission=false;
    63.                     rightThruster.enableEmission=false;
    64.                     GUI.Lose();
    65.                 }
    66.                
    67.                 GUI.UpdateFuelMeter(fuelAmount);
    68.                
    69.             }
    70.             else if(audio.isPlaying)
    71.             {
    72.                 audio.Stop();
    73.             }
    74.             }
    75.             }
    76.         }
    77. }
    78.  
     
    Last edited: Sep 27, 2012
  6. Karmarama

    Karmarama

    Joined:
    Jul 11, 2011
    Posts:
    261
    I'm not sure why you're doing a check for a tag by one digit "0"....not how I'd be doing it, but anyway...

    As for your movement - are you correctly referencing the object being moved?
    Code (csharp):
    1.  
    2. var playerTarget : Transform; //input player from inspector here
    3.  
    4. function Update(){
    5.      if (info.transform.CompareTag("leftButton")){
    6.      playerTarget.transform.Translate(Vector3.forward * Time.deltaTime);
    7.      }
    8. }
    9.  
    Depends how you're wanting to move the object. Translation usually ignores collision, so it's probably not what you want.
    Ideally, your object (which I presume is a ship) would probably use a character controller w/ Move or SimpleMove.

    http://docs.unity3d.com/Documentation/ScriptReference/Transform.Translate.html
    http://unity3d.com/support/documentation/ScriptReference/Rigidbody.html
    http://docs.unity3d.com/Documentation/ScriptReference/CharacterController.html
     
  7. Budgieboy

    Budgieboy

    Joined:
    Sep 6, 2012
    Posts:
    40
    Thank for the help but I can't get the rocket to move :(
     
  8. Karmarama

    Karmarama

    Joined:
    Jul 11, 2011
    Posts:
    261
    What code did you try to move the rocket? How are you setting it up?