PlaceTargetWithMouse.cs 822 B

1234567891011121314151617181920212223242526272829303132
  1. using System;
  2. using UnityEngine;
  3. namespace UnityStandardAssets.SceneUtils
  4. {
  5. public class PlaceTargetWithMouse : MonoBehaviour
  6. {
  7. public float surfaceOffset = 1.5f;
  8. public GameObject setTargetOn;
  9. // Update is called once per frame
  10. private void Update()
  11. {
  12. if (!Input.GetMouseButtonDown(0))
  13. {
  14. return;
  15. }
  16. Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
  17. RaycastHit hit;
  18. if (!Physics.Raycast(ray, out hit))
  19. {
  20. return;
  21. }
  22. transform.position = hit.point + hit.normal*surfaceOffset;
  23. if (setTargetOn != null)
  24. {
  25. setTargetOn.SendMessage("SetTarget", transform);
  26. }
  27. }
  28. }
  29. }