Spawning Prefab (51487)

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.

That 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!

If 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).

Thanks, 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!

1 Answer

1

if(Input.GetKeyUp(KeyCode.Mouse1)){
Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
RaycastHit hitinfo = new RaycastHit();
if (Physics.Raycast (ray, out hitinfo, 100)) {
// Next line uncommented to generate object where clicked
Instantiate(myGameobject, hitinfo.point, Quaternion.identity);
// Next line uncommented to generate object in cube where clicked
//Instantiate(myGameobject, new Vector3((int) hitinfo.point.x, (int) hitinfo.point.y, (int) hitinfo.point.z), Quaternion.identity);
}
}

Oh, just to inform you: I’m working on something similar to your idea too…

EDIT: Fixed!

Thanks! This code doesnt seem to work for me, I am getting 3 different errors! - Use of unassigned local varible "hitinfo". - The best overloaded method match for UnityEngine.Physics.Raycast(UnityEngine.Ray, out UnityEngine.RaycastHit, float)' has some invalid arguments. - Argument #2' is missing `out' modifier. Since you are working on the same idea like me, maybe we should catch up and who knows, maybe we can teach each other some tricks and tips!

The fixed version did not work, but fixed 3 problames but gave me one; - Cannot convert null to `UnityEngine.RaycastHit' because it is a value type.

Uff... didn't you say you know how to code in C#? This is just because RaycastHit is probably a struct or something like and not a class. Anyway now it should be (finally) fixed! Have a nice day! :)

I do know how to code C#, since this is the only thing I have used in advance of Javascript, but not that I am pro in anyway! To be able to place blocks down like this, is my last step in the game to be able to throw out the alpha, so I would be very happy if someone could help me solve this, since position and direction is very new to me!

Well, then much has been done! Good work! And please send me a PM if you publish it! Thanks for your patience.