x


iOS touch triggers the wrong object

Hello. Can someone tell me what I'm doing wrong? I have several objects in a scene, and I want to be able to tap them to to manipulate them in different ways. Some of the objects are fine, I tap them and they animate/whatever. But when I tap some other objects, an animation in a completely different object occurs. I feel like I'm missing something simple here and I think I've come across this question here before but I can't find it now.

Please help me :)

EDIT: this is the code in the js-script attached to the object I tap and want to animate:

 function Update () {

}

function FixedUpdate() {
for(var touch : Touch in Input.touches) 
{
    var ray : Ray = Camera.main.ScreenPointToRay(touch.position);

    if(touch.phase == TouchPhase.Ended) 
    {

        var hit : RaycastHit;

        if (Physics.Raycast (ray, hit, 100.0)) {
        //Debug.Log("Touched...");
        //var curX = Input.GetTouch(0).position.x;
        //var curY = Input.GetTouch(0).position.y;
        //Pos = Instantiate(curX, curY, 1000); 
        //Pos.rigidbody.AddForce(transform.forward * speed);
        rigidbody.AddForce(0, 1200, 0);
    }
}
}
}

And this is the code in the cs-script attached to the object playing its animation:

    public class TouchBlueTeapot : MonoBehaviour {

    public Animation m_animation;

    // Use this for initialization
    void Start () {
       Debug.Log("started");
    }

    // Update is called once per frame
    void Update () {
    foreach (Touch touch in Input.touches)

{
    Ray ray = Camera.main.ScreenPointToRay(touch.position);
         Debug.Log("RAY:" + ray.direction);

    if (touch.phase == TouchPhase.Began)
    {
       Debug.Log("touchphase position:" + touch.position);
        RaycastHit hit = new RaycastHit();


        if (Physics.Raycast(ray, out hit, 1000))
        {


            Debug.Log("touched " + hit.transform.name);


              gameObject.animation.Play();

        }
    }
}


    }
}

Sorry if it looks kind of crappy...

more ▼

asked Apr 12 '12 at 12:45 PM

maryhary gravatar image

maryhary
0 2 3 5

Could you please provide the code you are currently using? Also, just as a precaution (because it seems to happen a lot), could you make sure that when you do copy in your code, highlight the entire code, and press the 101010 button in the toolbar to format it correctly. :)

Apr 12 '12 at 12:47 PM Kleptomaniac

Sure! Should I provide the code in the script attached to the object or also the script attached to the object being animated?

Apr 12 '12 at 12:56 PM maryhary
(comments are locked)
10|3000 characters needed characters left

0 answers: sort oldest
Be the first one to answer this question
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:

x3796
x2000
x581
x7

asked: Apr 12 '12 at 12:45 PM

Seen: 329 times

Last Updated: Apr 12 '12 at 01:07 PM