Grid Snap ingame problem

Im trying to snap an object in my scene to an plane, the objects is being dragged by the camera + mouse. The problem is, it does not snap to the Plane. And i guess this is because i have no grid script attached to my plane.

This is the code that is attached to the Main Camera:

using UnityEngine;
using System.Collections;

public class TileHandler : MonoBehaviour {

    public GameObject EditorPrefab;
    public Vector3 GridSize;

    // Use this for initialization
    void Start () {
   
    }
   
    // Update is called once per frame
    void Update () {
        Vector3 MouseWorld = GetComponent<Camera>().ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, -GetComponent<Camera>().transform.position.z - 5.0f));

        if (Input.GetKey(KeyCode.LeftControl))
        {
            EditorPrefab.transform.position = new Vector3(((int)(MouseWorld.x / GridSize.x)) * GridSize.x, ((int)(MouseWorld.y / GridSize.y)) * GridSize.y, -5.0f);
        }
        else
        {
            EditorPrefab.transform.position = MouseWorld;
        }

    }
}

As you can see here:

:roll_eyes:

Any help is appreciated!

Thanks!

Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);

Plane plane = new Plane(Vector3.up, Vector3.zero); // change the second parameter if your plane isn't aligned with the origin
float distance = 0f;

if(plane.Raycast(ray, out distance) == true)
{
    Vector3 point = ray.GetPoint(distance);

   // point now contains the location on the plane the mouse is over
}

Thanksfor the reply!

Do i put this on the plane? Or on the main camera where my first script also is attached too?
None of the both work :confused: