I\'m trying to figure out how Graphic.Raycaster works, but documentation doesn\'t help. I want to use it to cast raycast from some position at a certain angle and hit the U
From Unity docs:
The Graphic Raycaster is used to raycast against a Canvas. The Raycaster looks at all Graphics on the canvas and determines if any of them have been hit.
You can use EventSystem.RaycastAll to raycast against graphics(UI) elements.
Here is a short example for your case:
void Update() {
// Example: get controller's 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
// (e.g. the player's arm) to match the controller's 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 results = new List();
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 myself. So there might be some errors.
Here are some other references to help you understand more:
Hope it helps.