I have an AR scene that has one AR camera, an image target and a 3D object as below.
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:
EDIT 3
I add box collider and rigidbody to 3d object. Screenshot is below :
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.