void fireRaycast() { // Hit object RaycastHit hit; // Ray object that handles direction Ray ray = new Ray(transform.position, transform.rotation * Vector3.forward); if (Physics.Raycast(ray, out hit, maxCursorDistance)) { Debug.Log(hit.collider.name); gazeTime += Time.deltaTime; // Instantiate cursor at proper position and rotation cursorInstance.SetActive(true); cursorInstance.transform.position = hit.point; cursorInstance.transform.rotation = Quaternion.FromToRotation(Vector3.forward, hit.normal); // Invoke OnRaycastHit event if if (gazeTime > gazeTimeThreshold) { OnRaycastHit?.Invoke(hit); } } else { gazeTime = 0; cursorInstance.SetActive(false); } }
void Update() { // during left mouse button is held down if (Input.GetMouseButton(0)) { RaycastHit hit; Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); if (Physics.Raycast(ray, out hit)) { GameObject hitted = hit.collider.gameObject; OnRaycastHit raycastHit = hitted.GetComponent <OnRaycastHit>(); if (raycastHit) { raycastHit.RaycastHit(); } } } }
void Start() { OnRaycastHit rayHit = GetComponent <OnRaycastHit>(); // Subscribe ray cast enter message. rayHit.onRaycastEnterAsObservable.Subscribe(_ => { Debug.Log("Cylinder: onRaycastEnter"); }); // Subscribe ray cast stay message. rayHit.onRaycastStayAsObservable.Subscribe(_ => { Debug.Log("Cylinder: onRaycastStay"); }); // Subscribe ray cast exit message. rayHit.onRaycastExitAsObservable.Subscribe(_ => { Debug.Log("Cylinder: onRaycastExit"); }); }