x


i done this b4 but forgot :( touch screen

im sure all you know what this question is going to be lol, sorry for repeating. Anyone kind enuf to give a quick reminder as to why the code runs when touching anywhere on the screen? it shud run when i touch the gameobject with the collider=="Button"

function Update () 
{
    if( Input.touchCount > 0 )
    {
        var touch = Input.GetTouch(0);  // Cache touches for better performance
        var hit : RaycastHit;
        var ray = Camera.main.ScreenPointToRay( touch.position );

        // B E G A N
        if( touch.phase == TouchPhase.Began )
        {
            if( Physics.Raycast( ray, hit, 50 ) )   // If theres a hit
            {
                if( hit.collider.tag == "Button"  ) // If the hit is with button collider
                {
                    //be happy
                }
            }
        }
}
more ▼

asked May 01 '12 at 12:49 PM

samz gravatar image

samz
46 13 28 36

(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

I did a little check up on your code, assuming your tagged object is a gameObject (which it should be) then this should work I just broke up the act of finding the tagged object and the act of returning weather it has been hit or not for elegance and hopefully to get the code working. let me know.

 function Update () 
    {
        if( Input.touchCount > 0 )
        {

            var touch = Input.GetTouch(0);  // Cache touches for better performance
            var hit : RaycastHit;
            var ray = Camera.main.ScreenPointToRay( touch.position );

            // B E G A N
            if( touch.phase == TouchPhase.Began )
            {
                if( Physics.Raycast( ray, hit, 100 ) )   // If theres a hit
                {
                    var obj1 : GameObject = GameObject.FindWithTag("Button"); // Break up the task of finding the button and hitting it in two
                    if( hit.transform == obj1.transform  ) // If the hit is with button collider
                    {
                        //be happy
                    }
                }
            }
    }
more ▼

answered May 01 '12 at 02:04 PM

AtomicMarine gravatar image

AtomicMarine
504 42 49 70

BIG FAT VIRTUAL KISS! I LOVE YOU!!!!

May 01 '12 at 05:16 PM samz

@samz: stop spamming such posts as answers. Use comments!

May 01 '12 at 05:17 PM Bunny83

one second. i got 2 objects. when touched they are deleted. problem is you always have to touch object1 and delete before you can touch and delete object2 :S whats the deal with that? i want the script to delete which ever object is touched

function Update () 
{
    if( Input.touchCount > 0 )
    {
        var touch = Input.GetTouch(0);  // Cache touches for better performance
        var hit : RaycastHit;
        var ray = Camera.main.ScreenPointToRay( touch.position );

        if( Physics.Raycast( ray, hit, 100 ) )   // If theres a hit
        {
            var obj1 : GameObject = GameObject.FindWithTag("Button"); // Break up the task of finding the button and hitting it in two
            if( hit.transform == obj1.transform  ) // If the hit is with button collider
            {
                // B E G A N
               if( touch.phase == TouchPhase.Began )
                {
                    Destroy(obj1.gameObject);
                }
            }
        }
    }
}
May 01 '12 at 06:57 PM samz
(comments are locked)
10|3000 characters needed characters left

Using GameObject.FindWithTag is returning the first guy it finds, you could use FindGameObjectsWithTag, which will return a list of all objects with that tag:

var objects = GameObject.FindGameObjectsWithTag("Button");

then use

for (var object in objects)
{

if( hit.transform == object.transform  ){ // If the hit is with button collider

        //etc...
    }
    }
more ▼

answered May 01 '12 at 05:22 PM

Seth Bergman gravatar image

Seth Bergman
7k 10 16 28

0o0o0o0o thank youu

May 01 '12 at 06:59 PM samz
(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:

x1535
x790
x194
x65

asked: May 01 '12 at 12:49 PM

Seen: 536 times

Last Updated: May 01 '12 at 06:59 PM