Log in or register to post comments

touch an object

January 11, 2012 - 3:32am #1

i using the imagetargets..i want my object do some animation when i tap it..can someone help me??i'm using google nexus one..

Re: touch an object

March 29, 2012 - 6:05am #7

Yes, you are right, all I need is to add a collider.

Thanks!

Re: touch an object

March 23, 2012 - 10:38am #6

You need to add a collider. For the teapot, you can add a Mesh collider using the Component > Physics > Mesh Collider option. If you want to add a collider to the target you'll need to place a plane on the target to just cover it.

- Kim

Re: touch an object

March 22, 2012 - 9:21pm #5

Kim, Is it enought to open ImageTarget sample unity project and add this script (Update method of a MonoBehaviour) to one of imagetargets.Or need to add a collider, or another codes to detect the touching ?

I wasn't able to do that with this sample ImageTargetStones and this code:

using UnityEngine;
using System.Collections;

public class TouchDetection : MonoBehaviour {

	// 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();
		//		gameObject.transform.position=new Vector3(gameObject.transform.position.x,
        //                                  100,
        //                                  gameObject.transform.position.z);
				
        if (Physics.Raycast(ray, out hit, 1000))
        {

            //never reach here
            Debug.Log("touched " + hit.transform.name);
	
			gameObject.transform.position=new Vector3(gameObject.transform.position.x,
                                          1,
                                          gameObject.transform.position.z);
        }
    }
}
	
	}
}

Re: touch an object

January 13, 2012 - 12:35pm #4

Try putting that in the Update method of any MonoBehaviour.

- Kim

Re: touch an object

January 11, 2012 - 10:31pm #3

can you give me a complete file ksiva??

Re: touch an object

January 11, 2012 - 5:19pm #2

Add a collider to your object and then use a script to capture the touch events, something like this:

foreach (Touch touch in Input.touches)
{
    Ray ray = Camera.main.ScreenPointToRay(touch.position);
    
    if (touch.phase == TouchPhase.Began)
    {
        RaycastHit hit = new RaycastHit();
        if (Physics.Raycast(ray, out hit, 1000))
        {
            Debug.Log("touched " + hit.transform.name);

            // Play animation here
        }
    }
}

- Kim

Log in or register to post comments