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): if(Input.acceleration.y >0) //Checking for right arrow key { rightThruster.enableEmission = false; leftThruster.enableEmission = true; tilt += Vector3.down *0.05; } if(Input.acceleration.y <0) //Checking for left arrow key { leftThruster.enableEmission = false; rightThruster.enableEmission = true; tilt += Vector3.up *0.05; } if(Input.acceleration ==0) //Checking if no horizontal keys down { rightThruster.enableEmission = false; leftThruster.enableEmission = false; } if(Input.acceleration.x >0) //Checking for up arrow key { topThruster.enableEmission = false; bottomThruster.enableEmission = true; speed += Vector3.forward *0.01; } if(Input.acceleration.x <0) //Checking for down arrow key { bottomThruster.enableEmission = false; topThruster.enableEmission = true; speed += Vector3.back *0.01;
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?
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.
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.
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): function Update () { if(haveFuel) { for (var touch : Touch in Input.touches){ if (touch.phase == TouchPhase.Began){ var userRay = Camera.main.ScreenPointToRay(touch.position); var info : RaycastHit; if(Physics.Raycast(userRay, info, 100.0f)) if(info.transform.CompareTag("rightbutton")) //Checking for right arrow key { rightThruster.enableEmission = false; leftThruster.enableEmission = true; tilt += Vector3.down *0.05; } if(info.transform.CompareTag("leftbutton")) //Checking for left arrow key { leftThruster.enableEmission = false; rightThruster.enableEmission = true; tilt += Vector3.up *0.05; } if(info.transform.CompareTag ==0) //Checking if no horizontal keys down { rightThruster.enableEmission = false; leftThruster.enableEmission = false; } if(info.transform.CompareTag("upbutton")) //Checking for up arrow key { topThruster.enableEmission = false; bottomThruster.enableEmission = true; speed += Vector3.forward *0.01; } if(info.transform.CompareTag("downbutton")) //Checking for down arrow key { bottomThruster.enableEmission = false; topThruster.enableEmission = true; speed += Vector3.back *0.01; } speed *= 0.95f; transform.Translate(speed, Space.Self); transform.Rotate(tilt, Space.Self); if(Input.GetAxis("Vertical")==0) //Checking if no vertical keys down { bottomThruster.enableEmission = false; topThruster.enableEmission = false; } if(info.transform.CompareTag!=0) { if(!audio.isPlaying) { audio.Play(); } fuelAmount-=(fuelBurnSpeed*Time.deltaTime); //check if fuel is being used. if(fuelAmount<=0) //if we ran out of fuel { fuelAmount=0; haveFuel=false; audio.Stop(); bottomThruster.enableEmission=false; //disable thruster particles topThruster.enableEmission=false; leftThruster.enableEmission=false; rightThruster.enableEmission=false; GUI.Lose(); } GUI.UpdateFuelMeter(fuelAmount); } else if(audio.isPlaying) { audio.Stop(); } } } } }
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): var playerTarget : Transform; //input player from inspector here function Update(){ if (info.transform.CompareTag("leftButton")){ playerTarget.transform.Translate(Vector3.forward * Time.deltaTime); } } 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