- Home /
Can someone fix this building and breaking code?
I have a building and breaking block code for this game like Minecraft that I'm making. I click and I can get stuck in blocks. Also Sometimes when I click my Player gets deleted and I can't do anything unless I exit the game. I don't know if it's the code or something Please Help.
var GrassBlock : Transform;
var StoneBlock : Transform;
var BlockSelected : float = 1;
var Range : float = 20;
function Update ()
{
if (Input.GetKeyDown(KeyCode.Q))
{
BlockSelected -= 1;
}
if (Input.GetKeyDown(KeyCode.E))
{
BlockSelected += 1;
}
if (Input.GetMouseButtonDown(0) || Input.GetMouseButtonDown(1))
{
var Hit : RaycastHit;
var LookingDirection = transform.TransformDirection(Vector3.forward);
if (Physics.Raycast(transform.position,LookingDirection, Hit, 20))
{
if (Input.GetMouseButtonDown(1))
{
if (BlockSelected == 1)
{
var GrassBlock : Transform = Instantiate(GrassBlock, Hit.collider.transform.position + Hit.normal.normalized, Quaternion.identity);
GrassBlock.tag = "GrassBlock";
}
if (BlockSelected == 2)
{
var StoneBlock : Transform = Instantiate(StoneBlock, Hit.collider.transform.position + Hit.normal.normalized, Quaternion.identity);
GrassBlock.tag = "StoneBlock";
}
}
else
{
Destroy(Hit.transform.gameObject);
}
}
}
}
Well I see that you're defining floats as a solid number, 1 and 10, ins$$anonymous$$d of 1.0f and 10.0f, however I don't know if that will do anything differently. Also, add in some debug logs for every if statement so you can see what the last thing that happened was before you get stuck. I'm thinking that it's getting a little stuck when it's trying to listen for Q, E, Left Click, and Right Click, because your statements aren't nested properly.
A hit normal is already normalized
Check if the block will be instantiated too close to the player, if so then don't instantiate it.
Before you destroy the gameObject, check to make sure it is not the player first.
Your answer
Follow this Question
Related Questions
Help! Saving blocks in a grid-based building system 0 Answers
MaK like building script help... 0 Answers
Trouble with Minecraft like building 0 Answers
Cannot convert 'UnityEngine.GameObject' to 'UnityEngine.Transform' 1 Answer
Road Texture Is Stretching 3 Answers