1
\$\begingroup\$

I have an AR scene that has one AR camera, an image target and a 3D object as below. enter image description here

I want to move the AR object to the position of the mouse click. I have tried taking the Input.mouseposition (screen position), converting it using ScreenToWorldPosition and moving the 3D object to this position. The object is moved, but not to the mouse click position.

How can I move the object to the position of the mouse click? My code is as follows:

Camera cam;
Vector3 target = new Vector3(0.0f, 10f,0.5f);
// Use this for initialization
void Start () {
if (cam == null)
    cam = Camera.main;
}
void Update()
{
    if (Input.GetMouseButtonDown(0)) {
        Debug.Log("MouseDown");

        Vector3 mousePos = Input.mousePosition;
        mousePos = cam.ScreenToWorldPoint(mousePos);
        GameObject.Find("Car1").gameObject.transform.position = mousePos;
    }                                                                
}

EDIT 1

To move 3d object with mouse, I tried this code. But it don't move the position of mouse click. enter link description here

EDIT 2

If I add a plane to scene, I can move to the position of mouse click only on the plane. The code is taken from here. But the plane prevents to show AR camera view. The screenshot is below:

enter image description here

EDIT 3

I add box collider and rigidbody to 3d object. Screenshot is below :

enter image description here

I used below code :

if (Input.GetMouseButtonDown(0)) {
            Debug.Log("mouseDown = " + Input.mousePosition.x + " " + Input.mousePosition.y + " " + Input.mousePosition.z);

        RaycastHit hit;
        var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
        if (Physics.Raycast(ray, out hit))
        {
            if (hit.rigidbody != null)
                GameObject.Find("Car1").gameObject.transform.position = hit.point;
            else 
                Debug.Log("Rigidbody is null");
        }

When mouse clicked, 3d object isn't moved the position of mouse click.

\$\endgroup\$
1
\$\begingroup\$

The mouse is in the screen coordinates. The screen is two dimensional. And you are trying to convert the 2d screen coordinates to 3d world coordinates. You need to specify the desired z position to the mouse position vector (you can think of it as the distance from the camera):

var mousePos = Input.mousePosition;
mousePos.z = 10f;
mousePos = Camera.main.ScreenToWorldPoint(mousePos);
GameObject.Find("Car1").gameObject.transform.position = mousePos;

The other problem is that you need to know the desired z position to specify it. This is not that difficult for 2d graphics in Unity, because z of an object is most likely constant (because 2d worlds are xy) and you could just set it from the previous objects position:

var mousePos = Input.mousePosition;
mousePos.z = 10f;
mousePos = Camera.main.ScreenToWorldPoint(mousePos);
var transform = GameObject.Find("Car1").gameObject.transform;
mousePos.z = transform.position.z;
transform.position = mousePos;

But it might be a problem in 3d. One of the options is to raycast from mouse position and take the point of the hit as the target position:

RaycastHit hit;
var ray = Camera.main.ScreenPointToRay(Input.mousePos);
if (Physics.Raycast(ray, out hit))
{
    if (hit.rigidbody != null)
        GameObject.Find("Car1").gameObject.transform.position = hit.point;
}

This technique, of course, requires 3d colliders in the scene that could be hit by the raycast. And, possibly, you might need to calculate an offset from the hit point, so the character will not move into collider.

\$\endgroup\$
  • \$\begingroup\$ Thanks your help. I am newbie user. And I am a bit confued. If I use second code, 3d object moves, but I can't see it on the scene. My scene doesn't have any collider. Should I add a collider unity gui to use raycast? \$\endgroup\$ – zakjma Apr 30 '15 at 8:56
  • \$\begingroup\$ @holazollil If you are using the code with the constant z, you could try bigger values (e.g. 100), smaller (e.g. 1) or negative values (e.g. -20) just to finally place the object in front of your camera. You need to add objects with colliders to your scene to use the code with raycast. If you don't have any colliders in your scene, the raycast will obviously hit nothing, so you can't get the position from it. Objects with colliders could be invisible (add an empty object, attach collider component to it, adjust collider size). \$\endgroup\$ – Vladimir Apr 30 '15 at 11:43
  • \$\begingroup\$ There are 3 code your text. I use second code. When the application starts, I see ed object. But I don't see it when clicked mouse. I write the coordinate of 3d object on console. After clicked, x-y coordinates is very big, so I can't see it in AR screen. The position before click is {0.0,1.9,0.0}. After click {200.8,610.0,0.0} \$\endgroup\$ – zakjma Apr 30 '15 at 12:27
  • \$\begingroup\$ I edit my question. Please, read it. \$\endgroup\$ – zakjma Apr 30 '15 at 12:52
  • \$\begingroup\$ Second code is for 2d. It is obviously not working in 3d, because the camera may be rotated. The first code should then work for you. Also be sure, that the original main camera is removed from the scene and your AR camera has the tag MainCamera set on it. \$\endgroup\$ – Vladimir Apr 30 '15 at 14:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.