Question by
Disco91 · Apr 23, 2016 at 09:47 AM ·
gameobjectinstantiateprefabrotategrid based game
Place prefab with mouse on to a grid based surface with a correct orientation. c#
So far my script allows me to place a prefab onto the surface of the cube.
However i would like the the prefab to snap to a grid and for it to orientate itself correctly in relation to the surface it is placed on (as seen in the image above - created in sketchup).
below is my current script i have so far (this just allows me to place prefabs and nothing else) -
using UnityEngine;
using System.Collections;
public class MouseController : MonoBehaviour {
public Vector3 mouseVector;
public Vector3 prefabVector;
public Rigidbody prefabCube;
void Update () {
Vector3 mouseVector=new Vector3(Input.mousePosition.x, Input.mousePosition.y, 0f);
if(Input.GetMouseButtonDown(0))
{
Ray ray = Camera.main.ScreenPointToRay (mouseVector);
RaycastHit hit;
if(Physics.Raycast(ray,out hit, 1000f)){
prefabVector = hit.point;
Instantiate(prefabCube,prefabVector,Quaternion.identity);
}
}
}
}
Thanks for the help!
placement.png
(83.8 kB)
Comment