Log in or register to post comments

Placing an object at the position of click on the ImageTarget.

March 3, 2011 - 1:04am #1

Hi,
i have been trying to place a gameobject on the position of touch on an ImageTarget in unity. I am able to place object on a surface in unity by getting the position from raycasthit. But it is not workin when i try to place the object on the imagetarget. Can anyone help me out?

Re: Placing an object at the position of click on the ImageTarge

March 22, 2011 - 8:21pm #11

You will probably want to set the parent of your newly instantiated object to the trackable it is attached to. This will ensure its renderer is turned off when that trackable is lost. See the TrackableScript.cs OnTrackingLost function for more details.

- Kim

Re: Placing an object at the position of click on the ImageTarge

March 22, 2011 - 7:30am #10

hi kim,
i tried accessing the objects i created dynamically from ontrackinglost function and disabled them. I gave a tag to the object im using for instantiating the new objects and used gameObject.FindGameObjectsWithTag
for getting the objects . But it is remaining on the screen if the tracking is lost suddenly. It looks as if it is sticking on to the screen when it lost track suddenly.

Re: Placing an object at the position of click on the ImageTarge

March 21, 2011 - 12:54pm #9

This isn't really a QCAR SDK question, but it's easy enough. The Instantiate call returns an Object, and Objects have a name parameter. Just give the object a name and you can look it up later.

- Kim

Re: Placing an object at the position of click on the ImageTarge

March 21, 2011 - 12:41pm #8

hi kim,
im creating a gameobjects dynamically using instantiate statement. How can i give a name to each gameobject created so that i can access it at a later stage from some other code?

Re: Placing an object at the position of click on the ImageTarge

March 18, 2011 - 6:38am #7

Take a look at the OnTrackingLost method in TrackableScript.cs. This method is called whenever tracking is lost. Currently, it disables the renderer component of all the children of the trackable, so if your objects are attached to the trackable they should be turned off automatically. You can edit the code here to handle other situations, e.g. searching for specific gameobjects to turn on/off.

- Kim

Re: Placing an object at the position of click on the ImageTarge

March 17, 2011 - 10:46pm #6

Hi kim,
I tried the above code and it worked fine for me. When i loose track of the imagetarget slowly, all the objects that i placed on the image taget disappears, but, when i loose track suddenly, all the objects that i placed on the image taget remain on the screen as if it is stuck on the screen surface.
What should i do to remove it whenever tracking is lost?

Re: Placing an object at the position of click on the ImageTarge

March 8, 2011 - 5:48am #5

thankx kim .it works.

Re: Placing an object at the position of click on the ImageTarge

March 7, 2011 - 7:27am #4

I can get this to work with the following script, placed on the chips target in the ImageTargets sample project:

using UnityEngine;
using System.Collections;

public class TouchScript : MonoBehaviour
{
    public GUIText message;
    public Transform teapot;
    
    void Update ()
    {
        Plane targetPlane = new Plane(transform.up, transform.position);
        
        foreach (Touch touch in Input.touches)
        {
            Ray ray = Camera.main.ScreenPointToRay(touch.position);
            float dist = 0.0f;
            targetPlane.Raycast(ray, out dist);
            Vector3 planePoint = ray.GetPoint(dist);
            
            message.text = "Projected point: " + planePoint;
            Instantiate(teapot, planePoint, Quaternion.identity);
        }
    }
}

I created a GUIText object and attached it to the script, and then I attached the teapot model to the script. The teapot is neither textured nor rotated correctly, but it is created at the correct touch location.

Make sure you are tracking off the chips target, in fact you may want to delete the stones target just to be safe.

- Kim

Re: Placing an object at the position of click on the ImageTarge

March 6, 2011 - 11:46pm #3

Hi Kim,
I tried doing that. Im able to get the value for distance ,but it is in terms of E, ie, like 2.3545E+13 . the position i get using Getpoint is also havin the same. I tried creating an object there using the instantiate method giving this as the position , but im not able to see the object. What can be the problem?

Re: Placing an object at the position of click on the ImageTarge

March 3, 2011 - 5:47am #2

I might suggest doing a raycast against a Plane object. Create a plane from the transform of your ImageTarget object:

Plane targetPlane = new Plane(transform.up, transform.position);

That code works for a script attached to the ImageTarget, or you could pass in the ImageTarget object to a script that lives elsewhere.

Now do a raycast on the plane, with the current touch:

Ray ray = Camera.main.ScreenPointToRay(touch.position);
float dist = 0.0f;
targetPlane.Raycast(ray, out dist);
Vector3 planePoint = ray.GetPoint(dist);

Try that and let me know if it helps.

- Kim

Log in or register to post comments