- Home /
Minecraft Clone Block Placing Script - need help
hello , im trying to make a minecraft clone and im working on the placing system i made a script it works , when i try to place a block from above a block it works perfectly but when i try to place a block from the side the block is set inside the block i clicked i hit and same happens when i place a block from bellow, if you can help me or give me a working code to base my code on that would be great , thanks for reading.
var DistaneFired : int;
var ShootFrom : Transform;
var CurrentItem : GameObject;
function Update ()
{
CurrentItem.SetActive(true);
if(Input.GetMouseButtonUp(0))
{
var hit : RaycastHit;
var Direction : Vector3 = transform.TransformDirection(Vector3.forward);
Debug.DrawRay(ShootFrom.position,Direction*DistaneFired,Color.blue);
if(Physics.Raycast(ShootFrom.position, Direction,hit,DistaneFired))
{
hit.transform.tag="occ_des";
}
}
if(Input.GetMouseButtonUp(1))
{
var use : RaycastHit;
var Direction2 : Vector3 = transform.TransformDirection(Vector3.forward);
var hitrotatition = Quaternion.FromToRotation(Vector3.up, use.normal);
Debug.DrawRay(ShootFrom.position,Direction2*DistaneFired,Color.yellow);
if(Physics.Raycast(ShootFrom.position, Direction2,use,DistaneFired))
{
if(CurrentItem.name!="pickaxe" && CurrentItem.name!="Sword")
{
if(use.transform.name!="Collider(Clone))
{
print(use.point);
use.point.x=Mathf.Round(use.point.x);
use.point.z=Mathf.Round(use.point.z);
use.point.y+=0.5;
print(use.point);
Instantiate(CurrentItem, use.point,hitrotatition);
}
}
}
}
}
ps occ_des is the tag i use to destroy blocks, all the problems are at the use part
Answer by robertbu · Nov 06, 2014 at 05:19 PM
If this is how your original code is formatted, I highly encourage you to adopt an indentation style for your brackets that allows you to see the structure of your code. Long-term putting everything on the left margin will lead to many hours of searching for bugs and make it difficult for other to help you.
As for your problem, I assume you are instantiating cubes. This is not how Minecraft is done, but I believe this will fix your code:
print(use.point);
var pos = use.transform.position;
pos += use.normal * 1.0;
print(pos);
Instantiate(CurrentItem, pos,hitrotatition);
The 1.0 can be removed if you are using unscaled built-in cubes. For other objects, you may have to use some factor.
Your answer
Follow this Question
Related Questions
Creating blocks script! Need help :) 1 Answer
Place cubes? problem with my script :S 1 Answer
Audio Help! 1 Answer
Anyone got a Voxel generator I can use? 0 Answers
UV mapping 5 Answers