How to use graphic Raycaster with WorldSpace?

I am trying to understand how Graphic.Raycaster works, but the documentation does not help. I want to use it to aim the rays from some position at a certain angle and hit the interface. Another thing is that I do not know how to make it interact with the user interface (drag, click, etc.). I know this extensive topic, but I just can’t find a good explanation of how to use it, so I will be grateful for any explanation.

+4
source share
2 answers

From Unity docs:

Graphic Raycaster is used for raycast vs Canvas. Raycaster looks at all the graphics on the canvas and determines if they were hit.

You can use EventSystem.RaycastAll for raycast for graphical user interface (UI) elements.

Here is a short example for your case:

 void Update() { // Example: get controller current orientation: Quaternion ori = GvrController.Orientation; // If you want a vector that points in the direction of the controller // you can just multiply this quat by Vector3.forward: Vector3 vector = ori * Vector3.forward; // ...or you can just change the rotation of some entity on your scene // (eg the player arm) to match the controller orientation playerArmObject.transform.localRotation = ori; // Example: check if touchpad was just touched if (GvrController.TouchDown) { // Do something. // TouchDown is true for 1 frame after touchpad is touched. PointerEventData pointerData = new PointerEventData(EventSystem.current); pointerData.position = Input.mousePosition; // use the position from controller as start of raycast instead of mousePosition. List<RaycastResult> results = new List<RaycastResult>(); EventSystem.current.RaycastAll(pointerData, results); if (results.Count > 0) { //WorldUI is my layer name if (results[0].gameObject.layer == LayerMask.NameToLayer("WorldUI")){ string dbg = "Root Element: {0} \n GrandChild Element: {1}"; Debug.Log(string.Format(dbg, results[results.Count-1].gameObject.name,results[0].gameObject.name)); //Debug.Log("Root Element: "+results[results.Count-1].gameObject.name); //Debug.Log("GrandChild Element: "+results[0].gameObject.name); results.Clear(); } } } 

The above script is not tested by me. Thus, there may be some errors.

Here are a few other links to help you understand more:

Hope this helps.

+5
source

The current proposal of Umair M does not take into account the fact that the beam occurs in world space and moves at an angle.

It doesn't seem to me that you can make the GUI in world space at an angle, even if your canvas is in world space. This page offers a method of creating a camera without rendering, moving it in 3D space using the beam you want to cast, and then make a graphical graphical interface relative to this camera. I haven't tried it yet, but that sounds promising.

+1
source

Source: https://habr.com/ru/post/1264555/


All Articles