简体   繁体   中英

Unity - Move Canvas as a 3D GameObject

i have a script with which i can move (on 2 axis) any 3D objects, the script i'm using is this (for mouse and touchscreen)

using UnityEngine;
public class DragInput : MonoBehaviour {



    GameObject gObj = null;
    Plane objPlane;
    Vector3 mO;
    Camera cam;

    void Awake() {
        cam = GameObject.FindGameObjectWithTag("MyCamera").GetComponent<Camera>();
    }
    Ray GenerateMouseRay()
    {
        Vector3 mousePosFar = new Vector3(Input.mousePosition.x,Input.mousePosition.y,cam.farClipPlane);
        Vector3 mousePosNear = new Vector3(Input.mousePosition.x,Input.mousePosition.y,cam.nearClipPlane);
        Vector3 mousePosF = cam.ScreenToWorldPoint(mousePosFar);
        Vector3 mousePosN = cam.ScreenToWorldPoint(mousePosNear);

        Ray mr = new Ray(mousePosN, mousePosF-mousePosN);
        return mr;
    }

    // Update is called once per frame
    void Update () 
    {
        if(Input.GetMouseButtonDown(0))
        {
            Ray mouseRay = GenerateMouseRay();
            RaycastHit hit;

            if (Physics.Raycast(mouseRay.origin, mouseRay.direction, out hit))
            {
                gObj = hit.transform.gameObject;
                objPlane = new Plane(cam.transform.forward*-1, gObj.transform.position);

                //calc mouse offset
                Ray mRay = cam.ScreenPointToRay(Input.mousePosition);
                float rayDistance;
                objPlane.Raycast(mRay, out rayDistance);
                mO = gObj.transform.position - mRay.GetPoint(rayDistance);
            }
        }
        else if(Input.GetMouseButton(0) && gObj)
        {
            Ray mRay = cam.ScreenPointToRay(Input.mousePosition);
            float rayDistance;
            if (objPlane.Raycast(mRay, out rayDistance))
                gObj.transform.position = mRay.GetPoint(rayDistance) + mO;
        }
        else if (Input.GetMouseButtonUp(0) && gObj)
        {
            gObj = null;
        }
    }
}

If i click on any object i have created (with a box collider) i can freely move this object over the screen. Unfortunately this will not happen if the object is a World Space UI(canvas). Anyone can gime me some hint and tell me why i'm not able to move the space UI?

You can have a look at RectTransform :

What happens is that if you want to move a UI element, this element is moved in relation to it's parent Canvas object.

It's position is a Vector2 on the Canvas area. It's a 2D element, even if it's placed in 3D space. Also, pay attention to the fact that it's position is also defined in relation to it's Anchor on the Canvas ( anchoredPosition ) :

You can parent the Canvas under a regular GameObject if you want to manipulate it's Transform like you would do for any other GameObject in the scene.

You can parent the Canvas under a regular GameObject if you want to manipulate it's Transform like you would do for any other GameObject in the scene

It was a good idea but i had to add a 3D collider too to the empty object, 2D collider did not work (and still do not understand why)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM