Attaching Object to mouse


I’m working on a RTS and I have some trouble getting the object move smooth. It’s following the mouse correctly but very jumpy at some point.

Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
			Physics.Raycast (ray, out hit, Mathf.Infinity);

			if(hit.collider.gameObject.tag == "Ground"){
				attachedObject.transform.position = new Vector3(hit.point.x,hit.point.y+3f,hit.point.z);
				hittedGround = hit;

				if(hit.collider.gameObject.tag == "layout" && transform.position!=hit.point){
					attachedObject.transform.position = new Vector3(hit.point.x,hittedGround.point.y+3f,hit.point.z);

I’m using a Raycast to get the correct “y” of the terrain and saving that to be able when I hover over the object that I can still move it with the same “y”. But it seems between hitting the terrain and the object its a bit jumpy, like it gets moved to the position and after that moved to the correct “y”. It’s just an annoying effect. Maybe someone can help me.

I would do it so that when you click the object initially you turn on a bool in a script attached to the object, that defines that it has been picked up.
Then in that seperate script I would say if it is picked up, make the position = Input.mousePosition. Right now it looks like you’re raycasting it even when it’s already picked up, which means if you move the mouse too quickly during one frame, it will fall out of the raycast?
Remember also to make sure that the object doesn’t collide with anything else in the scene if it’s not supposed to do that.