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); } }
public void Cast() { Ray ray; if (camera) { ray = camera.ScreenPointToRay(Input.mousePosition); } else { ray = new Ray(transform.position, ((directionSpace == DirectionSpace.Local) ? (transform.TransformDirection(direction)) : (direction))); } RaycastHit hit; if (Physics.Raycast(ray, out hit, distance, layerMask)) { Debug.DrawLine(ray.origin, hit.point, Color.green); onRaycastHit.Invoke(gameObject, hit.collider); } else { Debug.DrawLine(ray.origin, ray.origin + ray.direction * distance, Color.white); } }