ScreenToWorldPoint

Hi, I am able to instantiate game objects on click, but when I drag the game object to where I want it to go, it isn’t following my cursor very well at all. The below code is something I am using to try to follow the cursor, but there’s something wrong.

void Update () {

        Vector3 m = Input.mousePosition;
        m = new Vector3 (m.x, m.y, transform.position.y);
        Vector3 p = GetComponent<Camera> ().ScreenToWorldPoint (m);

The badly functioning end result is shown within a 14 second video below:

Thanks for any help in advance.

I have set instantiated objects to stay on the Plane beneath them, maybe this is why things look how they do. It would be good to keep the objects on the Plane while they can move around it better.

I assume you want the objects to be placed on the plane?
This is usually done by raycasting. ScreenToWorldPoint does not do this for you.

The last example at the bottom of that page shows how to cast a ray from mouse on screen into the world and find a point where something is hit.

Beware though, if your instantiated objects have colliders, these will also be hit, and the effect would be an object moving closer and closer to the camera. You can put the objects in the Ignore Raycast layer to solve this in the easiest way. But the raycast method has arguments to supply your own set of layers to hit, if you need to hit the spawned objects with other rays later.