- Home /
Prerendering a mesh before you place it on the world?
So I know how to place a prefab based on the mouse position using a ray-cast, but how do I render the mesh in the prefab where the mouse is pointing before I place it on the world?
Also if anyone could tell me how I could make it so I cant place an object if one is already there. Should I use some type of box collier on my prefab attached with a trigger? How do I make the collision perfect and will it cause to much overhead?
Thanks
Thanks, sorry this is my first time using the website. Thank you for the tips.
no worries, just saving you from getting downvoted. Happy Coding =]
Answer by sparkzbarca · Nov 21, 2012 at 07:47 PM
just actually spawn the object. move the object so its position is constantly the mouse position and disable collision so it shows up wherever. Make it semi transparent so the player realizes it hasn't been placed yet as far as the game is concerned.
When the player left clicks detach it and enable collision but first check to ensure nothing is in the way. If something is in the way don't allow it to detach or anything so they cant get it operating in the game.
perfect collision is done with an arbitrary shape by using a convex mesh collider. It does cause alot of overhead thats why most people don't make it perfect. :P
if you would like to know if something is there have a mesh collider or any collider and have it as a trigger not as a normal collider.
OnTriggerEnter(collider other)
{
canBuild = false;
}
OnTriggerStay(collider other)
{
canbuild = false;
}
OnTriggerExit(collider other)
{
Canbuild = true;
}
now when an object enters the meshs bounds and while it stays there it will set canbuild to false. you can use that bool to stop building.
You need the Ontrigger stay in case multiple objects are involved. you can't just used ontrigger enter and exit.
for example object a is inside mesh can build = false; object b is inside mesh can build = false; object a leaves the trigger can build = true; but object b is still in there, so you want the on trigger stay so after the object exits the object b sets canbuild to false again.
MARK AS ANSWERED have a good one. :)
thank you I didnt know about that Ontrigger stay. So I'm curious though, how do I make all my meshes in the game-world collide with this script without any overhead? Does everything have to be a trigger or can I use normal collisions?
Thanks
the only trigger is the collider BEFORE you spawn it into the world, during placement the collider should be a trigger.
other non-trigger colliders will cause the trigger collider to "trigger" as it were.
basically the difference between trigger and regular colliders is when regular colliders run into a collider they stop and when they run into a trigger they send a message but there movement isn't obstructed.
regular colliders define an area as occupied, trigger colliders just define it as specila.
Your answer
![](https://koobas.hobune.stream/wayback/20220613084309im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Non uniform movement radius 1 Answer
Raycast shooting with prefab 0 Answers
How do I instantiate a projectile along the path of a raycast? 0 Answers
Destroying a prefab with a prefab? 2 Answers
Click to Move With Navmesh 0 Answers