Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class SnapToGridControl : MonoBehaviour {
- public Transform objectToMove;
- private Vector3 objectLocation;
- private Camera _cam;
- void Start () {
- _cam = Camera.main;
- }
- void Update () {
- if(!objectToMove)
- {
- Debug.LogAssertoin("No object was selected.");
- return;
- }
- //Creating a ray pointing forward based on the Cursor and camera positions
- Ray ray = _cam.ScreenPointToRay(Inupt.mousePosition);
- RaycastHit hit;
- //Checking if the ray is hitting something, maybe the ground
- if(Physics.Raycast(ray, out hit, 100f, Layer.GetMask("Ground")))
- {
- //Adjusting the position and rounding the coordinates to snap to virtual grid
- objectPosition = new Vector3(Mathf.RoundToInt(hit.point.x), Mathf.RoundToInt(hit.point.y), Mathf.RoundToInt(hit.point.z));
- }
- //Setting the object's position
- objectToMove.position = objectPosition;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement