Instantiation problems

Hey guys. Not sure if this belongs in scripting or support, feel free to move the topic.

So i have in my game a script that allows me to instantiate a turret prefab, by clicking the mouse. The script raycasts from the center of the screen, and returns the point of intersection with the ground as the position of instantiation. That part of the script is seen here:

 if (Input.GetButtonDown("Fire1")){
		
		RaycastHit hit;
		 if (Physics.Raycast (Camera.main.transform.position,Camera.main.transform.forward, out hit,1000)) {
			 _position = hit.point;
			 Object tower;
				if(mTurret == true){tower = Instantiate(mturret, _position, Quaternion.identity);
					//Reset the Machinegun Turret bool
					mTurret =! mTurret;
					//return to idle state
		 			playerState = PlayerState.idle;
					}

The problem is, that the turret prefab keeps instantiating halfway through the floor as seen in this picture:


Red circle is how the turret is supposed to stand. yellow circle is how it actually stands

So i tried a few things:
-put a rigidbody on the turret prefab. didnt help
-Put a box collider around the prefab. didnt help
-moved the pivot of the model just down below the base plate. didnt help.

Im running out of ideas here, any tips?
thanks :slight_smile:

The problem is that hit.point is the location where the Ray hit something.

And that probably the origin of your object (the pivot point in your graphic application, max / maya / blender) is at the center of your object.

So, when you instantiate your object and put it to the location of the ray, it is the pivot center which goes to the location.

To clarify, it’s the object’s center that you need to move, not its pivot point. Alternatively, offset the position you instantiate at by the appropriate amount along the surface normal at hit.point (but moving the model’s center is probably easier :wink: