- Home /
Creating differently colored cubes, selecting by pressing 1,2,3,...
I want to have a system that allows me to select the color of the cubes by pressing 1,2,3,... and build them afterwards. Currently I only have this script:
var blockLayer : LayerMask = 1;
var range : float = Mathf.Infinity;
var hit : RaycastHit;
function Update () {
if (Input.GetMouseButtonDown(0))
Build();
if (Input.GetMouseButtonDown(1))
Erase();
}
function Build() {
if (HitBlock()) {
var cube = GameObject.CreatePrimitive(PrimitiveType.Cube);
cube.transform.position = hit.transform.position + hit.normal;
cube.name = "ErasableCube";
}
}
function Erase() {
if (HitBlock())
if(hit.transform.name == "ErasableCube"){
Destroy(hit.transform.gameObject);
}
}
function HitBlock() : boolean {
return Physics.Raycast(transform.position, transform.forward, hit, range, blockLayer);
}
I just want basic colors like red, green or blue.
Answer by Timseinelps · Dec 21, 2013 at 04:38 PM
Thats the code i used in the End:
var blockLayer : LayerMask = 1;
var range : float = Mathf.Infinity;
var hit : RaycastHit;
var select : int = 1;
function Update () {
if (Input.GetMouseButtonDown(0))
Build();
if (Input.GetMouseButtonDown(1))
Erase();
if (Input.GetButtonDown("1"))
select = 1;
if (Input.GetButtonDown("2"))
select = 2;
if (Input.GetButtonDown("3"))
select = 3;
}
function Build() {
if (select == 1){
if (HitBlock()) {
var cube = GameObject.CreatePrimitive(PrimitiveType.Cube);
cube.transform.position = hit.transform.position + hit.normal;
cube.name = "ErasableCube";
}
}
if (select == 2){
if (HitBlock()) {
cube = GameObject.CreatePrimitive(PrimitiveType.Cube);
cube.renderer.material.color = Color.red;
cube.transform.position = hit.transform.position + hit.normal;
cube.name = "ErasableCube";
}
}
if (select == 3){
if (HitBlock()) {
cube = GameObject.CreatePrimitive(PrimitiveType.Cube);
cube.renderer.material.color = Color.blue;
cube.transform.position = hit.transform.position + hit.normal;
cube.name = "ErasableCube";
}
}
}
function Erase() {
if (HitBlock())
if(hit.transform.name == "ErasableCube"){
Destroy(hit.transform.gameObject);
}
}
function HitBlock() : boolean {
return Physics.Raycast(transform.position, transform.forward, hit, range, blockLayer);
}
Answer by reefwirrax · Dec 20, 2013 at 04:41 AM
http://forum.unity3d.com/threads/180833-Unlit-Single-Color-Shader color,
maybe use a colored texture pic, and rewrite mesh.uv to equals a pixel on the texture, if above makes many drawcalls
Your answer
Follow this Question
Related Questions
I am trying to make this color picker script work 2 Answers
Get "on screen" color of a texture to use in a legend 2 Answers
Eyedropper/colordropper GameObject 0 Answers
How to loop through colors 2 Answers
Help,How to fix this? 0 Answers