Log in or register to post comments

Drag a gameObject only on a plane

April 8, 2020 - 8:07pm #1

Hi everybody. 



I have the gameobject like the one in the image I have uploaded and what I want to do is to drag the sphere(the one inside the red circle) into the rest of the gameobject.

The problem that I have is that if I move the camera, the sphere move together with the camera and it's not anymore aligned with the rest of the gameobject.

 

This is the code that I am using for the drag.

 

    private float dist;
    private Vector3 v3Offset;
    private Plane plane;
    public GameObject mainStructure;
 
    void OnMouseDown()
    {
      
        plane.SetNormalAndPosition(mainStructure.transform.up, mainStructure.transform.position);
        Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);     
        float dist;
        plane.Raycast(ray, out dist); 
        v3Offset = transform.position - ray.GetPoint(dist);
    }

    void OnMouseDrag()
    {
        Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
        float dist;
        plane.Raycast(ray, out dist);    
        Vector3 v3Pos = ray.GetPoint(dist);    
        Vector3 lastPos = v3Pos + v3Offset;      
        transform.position = plane.ClosestPointOnPlane(lastPos);  
    }




What I get with this code that the sphere will go back to the same plane once I hit about the mouse.

I hope I explained my problem right.





Thanks for your attention.

 

AttachmentSize
Image icon gameobject.png48.07 KB

Drag a gameObject only on a plane

April 9, 2020 - 1:01am #2

Hi,

In the Core Sample app available in the Asset Store, we demo Ground Plane. In the Ground Plane scene, you can move objects around independently from your camera.

Please go over our project.

Thank you.

Vuforia Engine Support

Log in or register to post comments