Greetings,
I just have this quick question about spawning prefabs, and I hope someone could please give me an example code that hopefully can do what I want to be done in my game!
What do I want to do?
- I want to spawn object like in Minecraft (please dont direct me to the minecraft starter package), when you right click an object. I have been able to spawn prefabs already, but I dont know how to make it spawn depending og where on the object you click.
Example:
- You have this cube in your world, and if you right click on the top of it, it will spawn the prefab simply on top of it. If you click on the side, it will spawn on that side of the object. Each object is 1 by 1 by 1 in radius, which means the object only takes 1 by 1 by 1 location on the map, so its not that hard to figure out what location the prefab is going to spawn, I just dont know how to code it..
This is my code so far in the script;
if(Input.GetKeyUp(KeyCode.Mouse1)){
Instantiate(myGameobject, new Vector3(10, 10, 10), Quaternion.identity);
}
I hope someone can help me whit this,
Best Regards,
Marius AW
You can't simply use prefabs to generate a cubic-like world, that would freeze the computer and drop the framerate, you have to use more sophisticated algorithms, sorry.
– bruce965That is not what I am trying to do, and not my question! Sorry, but thanks for the try! I am looking for the way to spawn prefabs where the position is depending on what side you are clicking another cube, just like in minecraft! To generate cubic-like world, like in minecraft, is very possible using prefabs, and will not drop your framrate or freeze your computer if you do it correctly!
– winsjansenIf you count on this, then: if(Input.GetKeyUp(KeyCode.Mouse1)){ var ray = Camera.main.ScreenPointToRay (Input.mousePosition); var hitinfo; if (Physics.Raycast (ray, hitinfo, 100)) { Instantiate(myGameobject, hitinfo.point, Quaternion.identity); } } hoping that there is no error (since I don't code in JS in Unity).
– bruce965Thanks, this seems to be more the answer to my question! :) I use C# and have little experiance whit javascript, could you "translate" the code to C#? and again, thanks!
– winsjansen