raycastTest.cs 760 B

12345678910111213141516171819202122232425262728
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. public class raycastTest : MonoBehaviour
  5. {
  6. bool showRay = false;
  7. // Start is called before the first frame update
  8. void Start()
  9. {
  10. }
  11. // Update is called once per frame
  12. void Update()
  13. {
  14. RaycastHit hit;
  15. Ray ray = new Ray(transform.position, transform.TransformDirection(Vector3.forward));
  16. if (Input.GetKey(KeyCode.Space)) {
  17. showRay = !showRay;
  18. }
  19. if (showRay && Physics.Raycast(ray, out hit)){
  20. Debug.DrawLine(transform.position, hit.point);
  21. Debug.DrawRay(hit.point, hit.normal * 50);
  22. Debug.Log("Distance is: " + hit.distance.ToString());
  23. }
  24. }
  25. }