x


GUITexture Touch Problem

I am trying to make a button for Android Input.Touch. But it doesn't work. Anyone can help please?

Here is the code:

using UnityEngine; using System.Collections;

public class AnInp : MonoBehaviour { public Texture2D myButton; // Use this for initialization void Start () { } // Update is called once per frame void Update () { Touch touch1; if(Input.touchCount > 0) { touch1 = Input.touches[0]; guiTexture.texture = myButton; if(touch1.phase == TouchPhase.Began && guiTexture.HitTest(touch1.position)) { Application.Quit(); }

} }

void OnGUI() { GUI.DrawTexture(new Rect(60,60,60,60), myButton, ScaleMode.StretchToFill, true, 10.0F); } /////////////////////////////////////////// }

more ▼

asked Aug 18 '12 at 02:25 PM

alee26 gravatar image

alee26
48 1 5 7

I'm not sure HitTest works in Android, since it has no MouseInput and HitTest is based on Mouse inputs. You should raycast your touch OR and much easier, use

if(GUI.Button(new rect(x,y,w,h),textureButton)){ Application.Quit(); }

p.s. this must of course be inside OnGUI() method.

Aug 18 '12 at 02:29 PM J.D.

Thanks for all. I need multitouch functions for a car and this will be the throttle in fact.

Aug 18 '12 at 09:21 PM alee26
(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

You're mixing different creatures: GUITexture and HitTest belong to the old gui system, while GUI.DrawTexture belongs to the new one - these systems are incompatible with each other, thus you should stick with one or the other.
To use a GUITexture, select the button texture in the Project view and click menu GameObject/Create Other/GUI Texture, than add this modified version of your code to it:

using UnityEngine; 
using System.Collections;

public class AnInp : MonoBehaviour { 

  void Update () { 
    Touch touch1; 
    if(Input.touchCount > 0) { 
      touch1 = Input.touches[0]; 
      if(touch1.phase == TouchPhase.Began && guiTexture.HitTest(touch1.position)) {
        Application.Quit(); 
      }
    } 
  }
}

To use the new GUI system, just follow @J.D.'s suggestion: use GUI.Button instead of GUI.DrawTexture:

using UnityEngine; 
using System.Collections;

public class AnInp : MonoBehaviour { 

  public Texture2D myButton;

  void OnGUI() { 
    if (GUI.Button(new Rect(60,60,60,60), myButton){
      Application.Quit();
    }
  }
}
more ▼

answered Aug 18 '12 at 03:57 PM

aldonaletto gravatar image

aldonaletto
42.5k 16 43 202

Thanks for the advice but the button will not quit the app indeed. It will be a throttle button for a car and i need a multitouch script. I will try the new gui system. P.S: Sorry for my bad english.

Aug 18 '12 at 09:18 PM alee26

I just tried the new gui system and the script. It was great man. Thanks a lot. I loved it. Now the question is can i put multiple guitextures from the gameobject/create menu and does it function multitouch(of coures until you answer me i will be testing:)). P.S: I am trying to make 4 touch buttons for accelaret, brake, left and right actions.

Aug 18 '12 at 09:45 PM alee26

For multitouch, I guess you must use GUITextures. There's another function, GUILayer.HitTest, that can tell which GUITexture is at some specific point - maybe you could use it for each touch, like this (script attached to an empty object):

using UnityEngine; 
using System.Collections;

public class AnInp : MonoBehaviour { 

  // drag the button GUITextures here:
  GUITexture accel;
  GUITexture brake;
  GUITexture left;
  GUITexture right;
  
  void Update () { 
    if (Input.touchCount > 0){ 
      foreach (Touch touch1 in Input.touches){
        // find the element touched:
        GUIElement button = GUILayer.HitTest(touch1.position);
        // verify which of your buttons is it:
        if (button == accel){ 
          // accelerator touch
        }
        else
        if (button == brake){
          // brake touch
        }
        else
        if (button == left){
          // steer left touch
        }
        else
        if (button == right){
          // steer right touch
        }
      }
    } 
  }
}
Aug 19 '12 at 01:13 AM aldonaletto

I'm on it at the moment. I will write the result. Thanks for helping me in this project. I have made a good car working on pc but i want to run it on Android and i am working for 10 ten days and had no proper result until your help. You are great :)

Aug 19 '12 at 09:09 PM alee26

I tried it right now and having this error: Assets/Scripts/AnInp.cs(16,38): error CS0120: An object reference is required to access non-static member `UnityEngine.GUILayer.HitTest(UnityEngine.Vector3)' I think (maybe i'm thinking wrong) GUILayer touch position has to be defined in vectors but how, or the "button" must be an object?

Aug 19 '12 at 09:26 PM alee26
(comments are locked)
10|3000 characters needed characters left
using UnityEngine;
using System.Collections;

[RequireComponent (typeof (Drivetrain))]

public class CarController : MonoBehaviour {

    public Wheel[] wheels;
    public Transform centerOfMass;
    public float inertiaFactor = 1.5f;
    public float brake;
    public float throttle;
    float throttleInput;
    float steering;
    float lastShiftTime = -1;
    float handbrake;

    Drivetrain drivetrain;
    CarController carengine;

    public float shiftSpeed = 0.8f;
    public float throttleTime = 1.0f;
    public float throttleTimeTraction = 10.0f;
    public float throttleReleaseTime = 0.5f;
    public float throttleReleaseTimeTraction = 0.1f;
    public bool tractionControl = true;
    public float steerTime = 1.2f;
    public float veloSteerTime = 0.1f;
    public float steerReleaseTime = 0.6f;
    public float veloSteerReleaseTime = 0f;
    public float steerCorrectionFactor = 4.0f;
    //lights
    public Light solFren;
    public Light sagFren;
    public Light solFar;
    public Light sagFar;
    public Light solStop;
    public Light sagStop;
    public GUITexture accelTouch;
    public GUITexture brakeTouch;
    public GUITexture leftTouch;
    public GUITexture rightTouch;

    public float slipVelo {
       get {
         float val = 0.0f;
         foreach(Wheel w in wheels)
          val += w.slipVelo / wheels.Length;
         return val;
       }
    }

    // Initialize
    void Start ()
    {
       if (centerOfMass != null)
       rigidbody.centerOfMass = centerOfMass.localPosition;
       rigidbody.inertiaTensor *= inertiaFactor;
       drivetrain = GetComponent (typeof (Drivetrain)) as Drivetrain;
    }

    public void Update () {
       if (Input.touchCount > 0){
       foreach (Touch touchControl in Input.touches){
       GUILayer touchLayer = Camera.current.GetComponent(typeof(GUILayer)) as GUILayer;
       GUIElement button = touchLayer.HitTest(touchControl.position);
              // Steering
       Vector3 carDir = transform.forward;
       float fVelo = rigidbody.velocity.magnitude;
       Vector3 veloDir = rigidbody.velocity * (1/fVelo);
       float angle = -Mathf.Asin(Mathf.Clamp( Vector3.Cross(veloDir, carDir).y, -1, 1));
       float optimalSteering = angle / (wheels[0].maxSteeringAngle * Mathf.Deg2Rad);
       if (fVelo < 1)
         optimalSteering = 0;

       bool steerLeft = (button == leftTouch);
       bool steerRight = (button == rightTouch);
       float steerInput = 0;

       if (steerLeft)
         steerInput = -1;
       if (steerRight)
         steerInput = 1;

       if (steerInput < steering)
       {
         float steerSpeed = (steering>0)?(1/(steerReleaseTime+veloSteerReleaseTime*fVelo)) :(1/(steerTime+veloSteerTime*fVelo));
         if (steering > optimalSteering)
          steerSpeed *= 1 + (steering-optimalSteering) * steerCorrectionFactor;
         steering -= steerSpeed * Time.deltaTime;
         if (steerInput > steering)
          steering = steerInput;
       }
       else if (steerInput > steering)
       {
         float steerSpeed = (steering<0)?(1/(steerReleaseTime+veloSteerReleaseTime*fVelo)) :(1/(steerTime+veloSteerTime*fVelo));
         if (steering < optimalSteering)
          steerSpeed *= 1 + (optimalSteering-steering) * steerCorrectionFactor;
         steering += steerSpeed * Time.deltaTime;
         if (steerInput < steering)
          steering = steerInput;
       }

       // Throttle/Brake
       bool accelKey = (button == accelTouch);
       bool brakeKey = (button == brakeTouch);

       if (drivetrain.automatic && drivetrain.gear == 0)
       {
         accelKey = (button == brakeTouch);
         brakeKey = (button == accelTouch);
       }

       if (accelKey)
       {
         if (drivetrain.slipRatio < 0.1f)
          throttle += Time.deltaTime / throttleTime;
         else if (!tractionControl)
          throttle += Time.deltaTime / throttleTimeTraction;
         else
          throttle -= Time.deltaTime / throttleReleaseTime;

         if (throttleInput < 0)
          throttleInput = 0;
         throttleInput += Time.deltaTime / throttleTime;
         brake = 0;
       }
       else 
       {
         if (drivetrain.slipRatio < 0.1f)
          throttle -= Time.deltaTime / throttleReleaseTime;
         else
          throttle -= Time.deltaTime / throttleReleaseTimeTraction;
       }
       throttle = Mathf.Clamp01 (throttle);

       if (brakeKey)
       {
         if (drivetrain.slipRatio < 0.1f)
          brake += Time.deltaTime / throttleTime;
         else
          brake += Time.deltaTime / throttleTimeTraction;
         throttle = 0;
         throttleInput -= Time.deltaTime / throttleTime;
         solFren.light.intensity = 5;
         sagFren.light.intensity = 5;
       }
       else 
       {
         if (drivetrain.slipRatio < 0.1f)
          brake -= Time.deltaTime / throttleReleaseTime;
         else
          brake -= Time.deltaTime / throttleReleaseTimeTraction;
         solFren.light.intensity = 0;
         sagFren.light.intensity = 0;
       }
       brake = Mathf.Clamp01 (brake);
       throttleInput = Mathf.Clamp (throttleInput, -1, 1);

       // Handbrake
       handbrake = Mathf.Clamp01 ( handbrake + (Input.GetKey (KeyCode.Space)? Time.deltaTime: -Time.deltaTime) );

       // Gear shifting
       float shiftThrottleFactor = Mathf.Clamp01((Time.time - lastShiftTime)/shiftSpeed);
       drivetrain.throttle = throttle * shiftThrottleFactor;
       drivetrain.throttleInput = throttleInput;

       if(Input.GetKeyDown(KeyCode.A))
       {
         lastShiftTime = Time.time;
         drivetrain.ShiftUp ();
       }
       if(Input.GetKeyDown(KeyCode.Z))
       {
         lastShiftTime = Time.time;
         drivetrain.ShiftDown ();
       }

       // Apply inputs
       foreach(Wheel w in wheels)
       {
         w.brake = brake;
         w.handbrake = handbrake;
         w.steering = steering;
       }
    }
       }
    }
    // Debug GUI. Disable when not needed.
    void OnGUI ()
    {

       GUI.Label (new Rect(0,60,100,200),"km/h: "+rigidbody.velocity.magnitude * 3.6f);
       tractionControl = GUI.Toggle(new Rect(0,80,300,20), tractionControl, "Traction Control (bypassed by shift key)");

    }
}
more ▼

answered Aug 25 '12 at 08:09 AM

alee26 gravatar image

alee26
48 1 5 7

Any more suggestions???

Aug 31 '12 at 09:00 PM alee26

I still need the answer, anyone can help please???

Sep 11 '12 at 08:58 PM alee26
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x2609
x606
x561
x494

asked: Aug 18 '12 at 02:25 PM

Seen: 1548 times

Last Updated: Sep 11 '12 at 08:58 PM