- Home /
Instantiate a Prefab with click in a certain area
First: I'm new here, and i'm not so good with english.
Ok. I already have a Script to spawn this prefab in a click. It's working perfect, but, I don't want to let the player spawn this prefab everywhere. Here's the code i'm using:
function Build() {
var mousePos = Input.mousePosition;
mousePos.z = Distance;
var objectPos = Camera.main.ScreenToWorldPoint(mousePos);
objectPos.y = objectPos.y + distanciachao;
Instantiate(Domino, objectPos, Quaternion.identity);
}
This make the prefab instatiate anywhere, but I Want to set a limit for this. I already have a box with the area I want for this Spawn. Any ideias?
$$anonymous$$aybe you can try with a raycaster that only reacts with your box so if you don't click on the box it just won't instantiate
Answer by Karsnen_2 · Jan 08, 2013 at 02:43 PM
Gabriel, how about you try to load the required amount prefabs at the Start and Transform they to an Unknown location.
Then try to move one by one to the required whenever needed.
This way, if it works then it is good.
Else if it does not work then there is an issue with "objectPos". Also verify for the Y position on objectPos. It might take a lot of height putting your GObject up the roof & no where to see.
Let me know if it works or I hope it at least helps you to find the issue.
Hey $$anonymous$$arsnen, thanks for the reply!
I wanna make clear that the script I put there is what is already working in my game. It's working perfect, spawning the prefabs I want where I click. The next question is HOW to put a way to put a limit in the area where I can click to Instantiate that Prefab. Now it's like: Trace where the mouse is looking, then Instantiate a Prefab when there's a click. What I want: Trace where the mouse is IN A CERTAIN AREA, then Instantiate a Prefab wheren there's a click IN THAT SA$$anonymous$$E CERTAIN AREA.
Hope that explains more what I'm trying to do ^^
Answer by plasticadmiral413 · Dec 14, 2017 at 10:24 AM
Put the instantiate function inside an "if" with a condition that only if mousePos is within a certain x & y. Your code will look somewhat like function Build() { var mousePos = Input.mousePosition; mousePos.z = Distance; var objectPos = Camera.main.ScreenToWorldPoint(mousePos); objectPos.y = objectPos.y + distanciachao; if(mousePos.x < YOUR LIMIT FOR X && mousePos.y < YOUR LIMIT FOR Y) { Instantiate(Domino, objectPos, Quaternion.identity); } }
Your answer
Follow this Question
Related Questions
Error: Instantiated Enemies don't get hit 2 Answers
Spawning unique prefabs at different transform posititions 0 Answers
How to spawn a 'boss' after all enemies defeated and then kill that 'boss'? 1 Answer
Give prefab a parent 2 Answers
Instantiate Prefab 0 Answers