im making a block based building game and im making it so when you press 1 and left click it places block 1, press 2 block2 etc.
so i dont know how to script it. heres my script.
CODE:
var blockLayer : LayerMask = 1;
var range : float = Mathf.Infinity;
var hit : RaycastHit;
var block1 : Transform;
var block2 : Transform;
var block3 : Transform;
var block4 : Transform;
var whichblock = 1;
function Block4 ()
{
if (Input.GetKeyDown(4))
var whichblock = 4;
}
function Block3 ()
{
if (Input.GetKeyDown(3))
var whichblock = 3;
}
function Block2 ()
{
if (Input.GetKeyDown(2))
var whichblock = 2;
}
function Block1 ()
{
if (Input.GetKeyDown(1))
var whichblock = 1;
}
function Update () {
if (Input.GetMouseButtonDown(0))
{
if (whichblock) == 1;
Build1();
if (whichblock) == 2;
Build2();
if (whichblock) == 3;
Build3();
if (whichblock) == 4;
Build4();
}
if (Input.GetMouseButtonDown(1))
Erase();
}
function Build1() {
if (HitBlock()) {
Instantiate (block1);
block1.transform.position = hit.transform.position + hit.normal;
}
}
function Build2() {
if (HitBlock()) {
Instantiate (block2);
block2.transform.position = hit.transform.position + hit.normal;
}
}
function Build3() {
if (HitBlock()) {
Instantiate (block3);
block3.transform.position = hit.transform.position + hit.normal;
}
}
function Build4() {
if (HitBlock()) {
Instantiate (block4);
block4.transform.position = hit.transform.position + hit.normal;
}
}
function Erase() {
if (HitBlock())
Destroy(hit.transform.gameObject);
}
function HitBlock() : boolean {
return Physics.Raycast(transform.position, transform.forward, hit, range, blockLayer);
}