Advertisement
Guest User

Untitled

a guest
Jun 26th, 2019
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.99 KB | None | 0 0
  1. public class SnapToGridControl : MonoBehaviour {
  2. public Transform objectToMove;
  3. private Vector3 objectLocation;
  4. private Camera _cam;
  5.  
  6. void Start () {
  7. _cam = Camera.main;
  8. }
  9.  
  10. void Update () {
  11. if(!objectToMove)
  12. {
  13. Debug.LogAssertoin("No object was selected.");
  14. return;
  15. }
  16.  
  17. //Creating a ray pointing forward based on the Cursor and camera positions
  18. Ray ray = _cam.ScreenPointToRay(Inupt.mousePosition);
  19. RaycastHit hit;
  20.  
  21. //Checking if the ray is hitting something, maybe the ground
  22. if(Physics.Raycast(ray, out hit, 100f, Layer.GetMask("Ground")))
  23. {
  24. //Adjusting the position and rounding the coordinates to snap to virtual grid
  25. objectPosition = new Vector3(Mathf.RoundToInt(hit.point.x), Mathf.RoundToInt(hit.point.y), Mathf.RoundToInt(hit.point.z));
  26. }
  27.  
  28. //Setting the object's position
  29. objectToMove.position = objectPosition;
  30. }
  31. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement