1
votes

Using Unity 4.1.2, using C# mono, Android Development.

Hi guys Ive created the following script.

using UnityEngine;
using System.Collections;

public class ButtonLeftMove : MonoBehaviour {

// Use this for initialization
public float MoveSpeed = 10;
private float min = -20f;
private float max = 20f;

void Start () {

}

// Update is called once per frame
void FixedUpdate () 
{
    MoveSpeed = 50F;
    GameObject joeblob = GameObject.FindGameObjectWithTag("playertag");

    if(Input.touches.Length > 0)
    {
        Debug.Log ("Touched");
        joeblob.rigidbody.AddForce(Vector3.left * MoveSpeed);
        //rigidbody.AddForce(Vector3.right * MoveSpeed);
    }

    Vector3 clampVel = joeblob.rigidbody.velocity;
    clampVel.x = Mathf.Clamp(clampVel.x, min, max);

    joeblob.rigidbody.velocity = clampVel;
}
}

Works great, object moves when the screen is touched, however this is true for any part of the screen.

this script is attached to my GUI texture, i only want it to be true if the GUI texture is touched Not anywhere on the screen!

what extra code am I missing?

cheers

1

1 Answers

4
votes

A GUITexture, which is a GUIElement has a method named HitTest. You can use this to test if your touch location is within your GUITexture. This would be something like

if(guiTexture.HitTest(Input.touches[0].position))
{
    //I'm hit, I'M HIT!! GOING DOWN!!!
}

Of course you'll have to make sure there actually are touches, but you get the idea.