Hi, I need to have a building follow the mouse, whist snapped to the surface of a low poly Icosphere. Im currently instantiating the prefab at the position of the mouse when hovering over the sphere (using raycasting) but the prefab seems to go crazy and fly towards the camera, rather than snapping to the face / normal of the mesh.
public GameObject objectPrefab;
private GameObject currentPrefab;
Ray ray;
RaycastHit hit;
// Update is called once per frame
void Update()
{
if (currentPrefab != null) {
CurrentPrefabMove ();
}
}
public void PlacePrefab(){
if (currentPrefab == null) {
currentPrefab = Instantiate (objectPrefab);
} else {
Destroy (currentPrefab);
}
}
private void CurrentPrefabMove(){
ray = Camera.main.ScreenPointToRay (Input.mousePosition);
if (Physics.Raycast (ray, out hit)) {
currentPrefab.transform.position = hit.point;
currentPrefab.transform.rotation = Quaternion.FromToRotation (Vector3.up, hit.normal);
}
}
This is the script which deals with that, any ideas?