Well,i have i script that places blocks and removes them. It has also an option to rotate desired cube.But when i try to rotate it 90 degrees it goes 179.xxxxxx .
Here is my script:
var placeLayer : LayerMask = 1;
var deleteLayer : LayerMask = 1;
private var range : float = Mathf.Infinity;
private var hit : RaycastHit;
private var hitt : RaycastHit;
var cubes : Transform[];
private var selectedCube : int = 0;
var playCloth : AudioClip[];
var playGrass : AudioClip[];
var playGravel : AudioClip[];
var playSand : AudioClip[];
var playSnow : AudioClip[];
var playStone : AudioClip[];
var playWood : AudioClip[];
var currBlock : String = "";
var positionCube : Rect;
var positionCubeGUI : Rect;
var textures : Texture2D[];
var parentt : Transform;
var placeBlockGUI : Texture2D;
var rot : int = 0;
function Update () {
positionCubeGUI = Rect(0, (Screen.height-130), 210, 130);
positionCube = Rect(12.5, (Screen.height-115), 100, 100);
Screen.lockCursor = true;
if (Input.GetMouseButtonDown(0))
Build();
if (Input.GetMouseButtonDown(1))
Erase();
if (Input.GetKeyDown(KeyCode.R)){
if (HitBlock()) {
hit.collider.transform.rotation.y += 1;
}
}
SelectBlock();
}
function Build() {
if (HitBlock()) {
var cube = Instantiate(cubes[selectedCube],hit.transform.position + hit.normal,Quaternion.identity);
currBlock = cube.name;
playSoundToCubesName();
}
}
function Erase() {
if (HitBlockin())
switch(hitt.transform.name){
case "cubeStone(Clone)":
playRandomBlockSound(5);
break;
case "cubeCobblestone(Clone)":
playRandomBlockSound(5);
break;
case "cubeDirt(Clone)":
playRandomBlockSound(1);
break;
case "cubeGrass(Clone)":
playRandomBlockSound(1);
break;
case "cubeDiamond(Clone)":
playRandomBlockSound(5);
break;
case "cubeGold(Clone)":
playRandomBlockSound(5);
break;
case "cubeIron(Clone)":
playRandomBlockSound(5);
break;
case "cubePlank(Clone)":
playRandomBlockSound(6);
break;
case "cubeGlass(Clone)":
playRandomBlockSound(5);
break;
/////////////////////////////////
case "cubeWoolBlack(Clone)":
playRandomBlockSound(0);
break;
case "cubeWoolBlue(Clone)":
playRandomBlockSound(0);
break;
case "cubeWoolBrown(Clone)":
playRandomBlockSound(0);
break;
case "cubeWoolDarkGreen(Clone)":
playRandomBlockSound(0);
break;
case "cubeWoolDarkPink(Clone)":
playRandomBlockSound(0);
break;
case "cubeWoolGreen(Clone)":
playRandomBlockSound(0);
break;
case "cubeWoolLightBlue(Clone)":
playRandomBlockSound(0);
break;
case "cubeWoolLightGrey(Clone)":
playRandomBlockSound(0);
break;
case "cubeWoolOceanBlue(Clone)":
playRandomBlockSound(0);
break;
case "cubeWoolOrange(Clone)":
playRandomBlockSound(0);
break;
case "cubeWoolPink(Clone)":
playRandomBlockSound(0);
break;
case "cubeWoolPurple(Clone)":
playRandomBlockSound(0);
break;
case "cubeWoolRed(Clone)":
playRandomBlockSound(0);
break;
case "cubeWoolYellow(Clone)":
playRandomBlockSound(0);
break;
}
Destroy(hitt.transform.gameObject);
}
function SelectBlock(){
if(Input.GetKeyUp(KeyCode.Keypad1)) selectedCube--;
if(Input.GetKeyUp(KeyCode.Keypad2)) selectedCube++;
if(selectedCube == cubes.length){
selectedCube = 0;
}
if(selectedCube < 0){
selectedCube = cubes.length-1;
}
}
function playSoundToCubesName(){
switch(currBlock){
case "cubeStone(Clone)":
playRandomBlockSound(5);
break;
case "cubeCobblestone(Clone)":
playRandomBlockSound(5);
break;
case "cubeDirt(Clone)":
playRandomBlockSound(1);
break;
case "cubeGrass(Clone)":
playRandomBlockSound(1);
break;
case "cubeDiamond(Clone)":
playRandomBlockSound(5);
break;
case "cubeGold(Clone)":
playRandomBlockSound(5);
break;
case "cubeIron(Clone)":
playRandomBlockSound(5);
break;
case "cubePlank(Clone)":
playRandomBlockSound(6);
break;
case "cubeGlass(Clone)":
playRandomBlockSound(5);
break;
/////////////////////////////////
case "cubeWoolBlack(Clone)":
playRandomBlockSound(0);
break;
case "cubeWoolBlue(Clone)":
playRandomBlockSound(0);
break;
case "cubeWoolBrown(Clone)":
playRandomBlockSound(0);
break;
case "cubeWoolDarkGreen(Clone)":
playRandomBlockSound(0);
break;
case "cubeWoolDarkPink(Clone)":
playRandomBlockSound(0);
break;
case "cubeWoolGreen(Clone)":
playRandomBlockSound(0);
break;
case "cubeWoolLightBlue(Clone)":
playRandomBlockSound(0);
break;
case "cubeWoolLightGrey(Clone)":
playRandomBlockSound(0);
break;
case "cubeWoolOceanBlue(Clone)":
playRandomBlockSound(0);
break;
case "cubeWoolOrange(Clone)":
playRandomBlockSound(0);
break;
case "cubeWoolPink(Clone)":
playRandomBlockSound(0);
break;
case "cubeWoolPurple(Clone)":
playRandomBlockSound(0);
break;
case "cubeWoolRed(Clone)":
playRandomBlockSound(0);
break;
case "cubeWoolYellow(Clone)":
playRandomBlockSound(0);
break;
}
}
function OnGUI()
{
GUI.DrawTexture(positionCubeGUI,placeBlockGUI);
GUI.DrawTexture(positionCube, textures[selectedCube]);
}
function playRandomBlockSound(block : int){
var wichOne : int = Random.Range(0, 3);
if(block == 0) AudioSource.PlayClipAtPoint(playCloth[wichOne],transform.position);
if(block == 1) AudioSource.PlayClipAtPoint(playGrass[wichOne],transform.position);
if(block == 2) AudioSource.PlayClipAtPoint(playGravel[wichOne],transform.position);
if(block == 3) AudioSource.PlayClipAtPoint(playSand[wichOne],transform.position);
if(block == 4) AudioSource.PlayClipAtPoint(playSnow[wichOne],transform.position);
if(block == 5) AudioSource.PlayClipAtPoint(playStone[wichOne],transform.position);
if(block == 6) AudioSource.PlayClipAtPoint(playWood[wichOne],transform.position);
}
function HitBlock() : boolean {
return Physics.Raycast(transform.position, transform.forward, hit, range, placeLayer);
}
function HitBlockin() : boolean {
return Physics.Raycast(transform.position, transform.forward, hitt, range, deleteLayer);
}