ShowMousePosition.cs 956 B

12345678910111213141516171819202122232425262728293031323334353637383940
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. public class ShowMousePosition : MonoBehaviour
  5. {
  6. public GameObject mousePointer;
  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. mousePointer.transform.position = snapPosition(getWorldPoint());
  15. }
  16. public Vector3 getWorldPoint()
  17. {
  18. Camera cam = GetComponent<Camera>();
  19. Ray ray = cam.ScreenPointToRay(Input.mousePosition);
  20. RaycastHit hit;
  21. if(Physics.Raycast(ray, out hit)) {
  22. return hit.point;
  23. }
  24. return Vector3.zero;
  25. }
  26. public Vector3 snapPosition(Vector3 original)
  27. {
  28. Vector3 snapped;
  29. snapped.x = Mathf.Floor(original.x + 0.5f);
  30. snapped.y = Mathf.Floor(original.y + 0.5f);
  31. snapped.z = Mathf.Floor(original.z + 0.5f);
  32. return snapped;
  33. }
  34. }