- Home /
Best way to snap items to ground or terrain.
Hey all I'm working on a script to place objects atm i get them into the scene and moving around but the objects sometimes start rising up into the air towards the camera or the objects flicker everywhere constantly and won't let me rotate. Im shooting a raycast from the camera to the terrain to work out rotation etc. Cursor transform position is a cube centred in the middle of my camera because unlike the tutorial i researched to do this i dont wanna use the mouse position to move the object i want it locked to the middle of the camera. The terrain I'm trying to place the objects on isnt that bumpy or anything as well its reasonably smooth and on a slight incline.
private void MoveCurrentObjectToStick()
{
Ray ray = camera.ScreenPointToRay(cursor.transform.position);
Debug.DrawRay(cursor.transform.position, camera.transform.forward, Color.green);
RaycastHit hitInfo;
if (Physics.Raycast(ray, out hitInfo))
{
currentPlaceableObject.transform.position = hitInfo.point;
currentPlaceableObject.transform.rotation = Quaternion.FromToRotation(Vector3.up, hitInfo.normal);
}
}
Any help would be appreciated cheers.
Your answer
Follow this Question
Related Questions
Flip over an object (smooth transition) 3 Answers
Orbiting objects appear stretched 1 Answer
Min, Max Rotation Help 2 Answers
how to combine 2 quaternion components? 2 Answers
Transform a Vector by a Quaternion 1 Answer