- Home /
Previewing mesh before instantiating
I'm trying to instantiate an object where a raycast hits and I already got that working but now I want to preview it before placing it. I've seen different posts saying that I have to instantiate the preview mesh as well, but I can't seem to instantiate it only once and make it follow the raycast end point.
bool IsBuilding;
[SerializeField]
private GameObject previewMesh;
[SerializeField]
private GameObject actualMesh;
[SerializeField]
private Camera camera;
private Vector3 origin;
private Vector3 direction;
// Use this for initialization
void Start () {
IsBuilding = false;
}
private void FixedUpdate()
{
origin = camera.transform.position;
direction = camera.transform.forward;
if (Input.GetKeyUp(KeyCode.Mouse2) && IsBuilding == false)
{
ActivateBuildMode();
}
else if (Input.GetKeyUp(KeyCode.Mouse2) && IsBuilding == true)
{
DeactivateBuildMode();
}
if (IsBuilding == true)
{
RaycastHit hit;
if (Physics.Raycast(origin, direction, out hit, 150) && hit.collider.tag == "Finish")
{
Instantiate(previewMesh, hit.point, hit.transform.rotation);
Debug.DrawRay(origin, direction, Color.red);
if (Input.GetKeyUp(KeyCode.Mouse0))
{
Instantiate(actualMesh, hit.point, hit.transform.rotation);
}
}
}
}
void ActivateBuildMode()
{
IsBuilding = true;
Debug.Log("Build Mode Activated");
}
void DeactivateBuildMode()
{
IsBuilding = false;
Debug.Log("Build Mode Deactivated");
}
}
Answer by Antao98 · Sep 01, 2018 at 12:29 PM
Solved it. Basically created a new boolean and defined the preview mesh in a game object. When I enter build mode it creates the preview mesh and changes the bool, and then I update the instantiated mesh's position. bool IsBuilding; [SerializeField] private GameObject previewMesh; [SerializeField] private GameObject actualMesh; [SerializeField] private Camera camera; private Vector3 origin; private Vector3 direction; bool isCreated = false; GameObject preview;
// Use this for initialization
void Start () {
IsBuilding = false;
}
private void FixedUpdate()
{
origin = camera.transform.position;
direction = camera.transform.forward;
if (Input.GetKeyUp(KeyCode.Mouse2) && IsBuilding == false)
{
ActivateBuildMode();
}
else if (Input.GetKeyUp(KeyCode.Mouse2) && IsBuilding == true)
{
DeactivateBuildMode();
}
if (IsBuilding == true)
{
RaycastHit hit;
if (Physics.Raycast(origin, direction, out hit, 150) && hit.collider.tag == "Finish")
{
if(isCreated == false)
{
preview = Instantiate(previewMesh, hit.point, hit.transform.rotation);
isCreated = true;
}
preview.transform.position = hit.point;
Debug.DrawRay(origin, direction, Color.red);
if (Input.GetKeyUp(KeyCode.Mouse0))
{
Instantiate(actualMesh, hit.point, hit.transform.rotation);
Destroy(previewMesh);
}
}
}
}
void ActivateBuildMode()
{
IsBuilding = true;
Debug.Log("Build Mode Activated");
}
void DeactivateBuildMode()
{
IsBuilding = false;
Debug.Log("Build Mode Deactivated");
}
}
Your answer
Follow this Question
Related Questions
i would like to create a preview of an instantiation but im haveing a problem with my code 1 Answer
How to make an Instantiated prefab a Child of a gameobject that already exists in the hierarchy 3 Answers
How to randomly instantiate other prefabs parallel? 1 Answer
What can prevent a prefab network instantiation? 0 Answers
my object keeps cloning is self i want it to clone once 2 Answers