- Home /
Placing a cube on the face of another
Something like minecraft
We're really going to need a more specific question to give you a proper answer. What exactly are you trying to do, what have you already tried (code samples or descriptions), what specifically isn't working / doesn't do what you expect, etc.
If you don't yet know enough about Unity to get started attempting to recreate $$anonymous$$inecraft, then I suggest you check out the Unity basics and tutorials:
http://unity3d.com/support/documentation/
FYI, my guess for why your question got downvoted is that it's vague and kind of comes across as you wanting someone else to do all the work for you. Since you're the game developer, it's up to you to figure out how your game should work, and the community is here to answer technical questions and respond to bugs, not to write tutorials on how to recreate $$anonymous$$inecraft in Unity.
Ive created unity games before and know how unity works I even make tutorials on youtube. I just need to know if how to like place a block on the face I click on. I have no code right now though.
In that case your question should at least mention which part you are having problems with: creating the cubes, positioning them, clicking on a cube, detecting which face was clicked or something else...
Breaking it in parts like that, maybe you already noticed you can do some of those or find answers to the ones you can't with search/google.
"created unity game*s* before"? If you have a little bit of Unity experience and a bit of general game development experience that's a quite simple question. Did you even search for something similar before? Just a search for "$$anonymous$$ecraft" on this site should be more than enough
guessing he means those silly drag and drop tutorials, or modifying the FPS island example that came out of the box
Answer by Bilelmnasser · Apr 20, 2017 at 09:31 AM
place this script inside your scene with a cube and click play :
void Update () {
if ( Input.GetMouseButtonDown( 0 ) )
{
Ray ray = Camera.main.ScreenPointToRay( Input.mousePosition );
RaycastHit hit;
if ( Physics.Raycast( ray , out hit , 2000 ) )
{
// it's better to find the center of the face like this:
Vector3 position = hit.transform.position + hit.normal ;
// calculate the rotation to create the object aligned with the face normal:
Quaternion rotation = Quaternion.FromToRotation( Vector3.up , hit.normal );
// create the object at the face center, and perpendicular to it:
GameObject Placement = GameObject.CreatePrimitive( PrimitiveType.Cube );
Placement.transform.position = position;
Placement.transform.rotation = rotation;
//Instantiate<( PrimitiveType.Cube as GameObject , position , rotation ) as GameObject;
}
else
{
Debug.Log( "nothing" );
}
}
}
Answer by Statement · Dec 28, 2011 at 04:11 AM
Not the most descriptive question I've seen... However, if you want some working example, here's a package I wrote some time ago, and I even have a blog post regarding some thoughts about it.
It contains a scene and scripts to generate voxels.
Answer by Julien-Lynge · Dec 27, 2011 at 07:07 PM
To do what you've asked, "place a block on the face I click on", here's what you will need:
http://unity3d.com/support/documentation/ScriptReference/Input-mousePosition.html
http://unity3d.com/support/documentation/ScriptReference/Camera.ScreenPointToRay.html
Collider.Raycast
(as well as http://unity3d.com/support/documentation/ScriptReference/RaycastHit.html for point and normal)
Object.Instantiate
Good luck!
It's much more useful to click the upvote-button than posting a comment that says "Thank you" ;)
Don't get it wrong, the comment is really nice, but a Q&A site should offer the best solution (Answer) first. We as the community are responsible to judge what's useful and what's not.
Be careful with downvotes and if you do so always post a comment which explains what's wrong. Downvotes should only be used when it clearly wrong.
Creating some sort of discrete coordinate system/grid system may also be relevant, i.e., to figure out the coordinate in which to spawn the other cube.
Answer by Jure · Dec 28, 2011 at 03:33 AM
I made a prefab out of cubes that are allready in unity and this is a code to spawn them in 2D grid. There is also few lines which make middle tile pruple and align camera to the center:
var tilePrefab : GameObject;
var y : int = 10;
var x : int = 10;
var initialColor : Color;
function Start (){
for (var i : int = 0;i < y; i++) {
for (var j : int = 0; j < x; j++)
{
var tile : GameObject = Instantiate (tilePrefab, Vector3(j, i, 0), Quaternion.identity);
if (i == y/2 && j == x/2)
{
print("test");
var pos: Vector3 = tile.transform.position; // get the tile position...
pos.z = Camera.main.transform.position.z; // but keep the camera z
Camera.main.transform.position = pos; // move camera to the position
tile.renderer.material.color = Color.cyan;
8//,b
}
}
}
}