- Home /
How do you make collision bounds using colour?
Firstly, can you even detect colour in unity ? It must be possible (through a add-on.) but it would take a tonne of coding to make a add-on. I would be suprised if this is not possible in Unity considering it is possible using BYOB (which uses javascript). You can download BYOB ( search in google)and look at what they use. I know it would definitely be possible to make collision bounds using the coordinates of where the colour is. (complex collision bounds in gameobjects)
As you may be able to see, the image i am using attached below has no background and it is literally just the black part. Could i use this to create a complex collision bound ( maybe using child gameobjects)?
If you are making a add-on i only need the colour black in collision bounds( could make it easier to code).
The reason i want this is because i have about 80 different mazes that i need to create collision bounds for, and each is about 70 box colliders. so 70x80 is 5600 colliders and that is a awful lot of work. I will be needing only box colliders to work, and although there are some mazes with curved sides, i can change them if they arent right. Also, if the odd one or two box colliders are glitchy, it doesnt matter, i just want most of the work to be done.
Any help will be appreciated. Also, if you do post a script or add-on or something, then please can i have full permissions to use it in my app? Thanks.
The picture you have there is for humans. A computer picture would just have a dot for a wall, and empty white for open.
You could write something to scan the pixels in each 5x5(?) section and check for more black (wall) or white and create a 2D array of wall/free. That's sort of what Graham is suggesting. But it sounds like you may have to learn more program$$anonymous$$g, first.
i attached the script onto my maze ( no option to put object in inspector tagged with script) and it came with these errors.
Assets/fixed scripts/collision bounds.js(4,28): BCE0044: expecting EOF, found 'y'.
Assets/fixed scripts/collision bounds.js(4,27): UCE0001: ';' expected. Insert a semicolon at the end.
Assets/fixed scripts/collision bounds.js(4,23): UCE0001: ';' expected. Insert a semicolon at the end.
Assets/fixed scripts/collision bounds.js(4,22): BCE0043: Unexpected token: ,.
Assets/fixed scripts/collision bounds.js(4,21): BCE0044: expecting ), found 'x'.
Assets/fixed scripts/collision bounds.js(4,7): UCE0001: ';' expected. Insert a semicolon at the end.
Assets/fixed scripts/collision bounds.js(2,32): UCE0001: ';' expected. Insert a semicolon at the end.
Assets/fixed scripts/collision bounds.js(2,10): BCE0043: Unexpected token: GameObject.
Assets/fixed scripts/collision bounds.js(1,9): BCE0043: Unexpected token: Texture2D.
Any help will be appreciated. Thanks.
Answer by Virenz · Apr 01, 2015 at 02:31 PM
MakakWasTaken has made the following codes (and emailed them to me) that will work if you have a extremely fast computer/laptop. Unfortunately, it doesnt work for me right now but it will probably work once my new laptop arrives!!! Then i should be able to keep it in the game file so it is not in runtime!!!:
public var image : Texture2D;//Just assign your image in the inspector.
private var CubeManager : GameObject;
function GetColor(x : int, y : int) { //Get the color at a specific coordinate
x = Mathf.Clamp(x,0,image.width);
y = Mathf.Clamp(y,0,image.height);
return image.GetPixel(x,y);
}
function Start() {
CubeManager = new GameObject(); //So your hierachy doesn't get flooded you make them a child of the CubeManager gameobject.
CubeManager.name = "CubeManager";
GenerateMap();
CubeManager.AddComponent(MeshCombiner);
}
function GenerateMap() {
for (var x : int = 0; x < image.width; x++) {
for (var y : int = 0; y < image.width; y++) {
var cube : GameObject = GameObject.CreatePrimitive(PrimitiveType.Cube);
cube.transform.parent = CubeManager.transform;
cube.GetComponent.<Renderer>().material.color = GetColor(x,y);
if (GetColor(x,y) == Color.white) {
cube.transform.position = new Vector3(x,0,y);
} else {
cube.transform.position = new Vector3(x,1,y);
}
}
}
}
and:
import System.Collections.Generic; //Used for the list
function Start () {
var meshFilters : MeshFilter[] = GetComponentsInChildren(MeshFilter);
var combine : CombineInstance[] = new CombineInstance[meshFilters.Length];
for (var i = 0; i < meshFilters.Length; i++){
combine[i].mesh = meshFilters[i].sharedMesh;
combine[i].transform = meshFilters[i].transform.localToWorldMatrix;
meshFilters[i].gameObject.active = false;
}
transform.GetComponent.<MeshFilter>().mesh = new Mesh();
transform.GetComponent.<MeshFilter>().mesh.CombineMeshes(combine);
transform.gameObject.active = true;
}
Answer by Graham-Dunnett · Mar 27, 2015 at 03:29 PM
Reading docs is always a good plan:
http://docs.unity3d.com/ScriptReference/Texture2D.GetPixel.html
So, yes, you can read a colour from a texture. So, you could iterate over each pixel, and if it's not white create a cube in 3d space.
firstly, will getpixels32 or getpixels be better? Secondly, how would i create a collision bound using this data? And thirdly, could you please write out the code i would need and how to use it (e.g: put in editor folder or attach to picture)(i am a noob at this, still learning, so sorry about writing the code for me, if you dont want to do it that is fine, just give me some guidence, any help is appreciated, thanks)
If depends if you want
Color
orColor32
colour values. 2. You iterate over each pixel in x and y. If you are at pixel 10,10, then the cube you create needs to be 1 unit by 1 unit by 1 unit, and located at 10,10 in world coordinates. 3. P$$anonymous$$ me on the forum, and we can discuss the cost for my coding services. ;-) Alternatively, work out if you want the create these maps at runtime, in which case the textures might be in a Resources folder, or created in the editor, in which case they might be in the Editor folder, and have some scripts that create the runtime data.
Thanks, i will try and return the results to you tomorrow.
I attached it to my maze (no option to attach maze to script in inspector) and it came up with these errors
Assets/fixed scripts/collision bounds.js(4,28): BCE0044: expecting EOF, found 'y'.
Assets/fixed scripts/collision bounds.js(4,27): UCE0001: ';' expected. Insert a semicolon at the end.
Assets/fixed scripts/collision bounds.js(4,23): UCE0001: ';' expected. Insert a semicolon at the end.
Assets/fixed scripts/collision bounds.js(4,22): BCE0043: Unexpected token: ,.
Assets/fixed scripts/collision bounds.js(4,21): BCE0044: expecting ), found 'x'.
Assets/fixed scripts/collision bounds.js(4,7): UCE0001: ';' expected. Insert a semicolon at the end.
Assets/fixed scripts/collision bounds.js(2,32): UCE0001: ';' expected. Insert a semicolon at the end.
Assets/fixed scripts/collision bounds.js(2,10): BCE0043: Unexpected token: GameObject.
Assets/fixed scripts/collision bounds.js(1,9): BCE0043: Unexpected token: Texture2D.
Any help will be appreciated. Thanks. Also, this is for android, so will it make any difference to the scripting?
P.S: Sorry for late response, i sent this yesterday but a moderator declined it for some reason, so i decided to comment on this answer so i cant get declined again
Answer by MakakWasTaken · Mar 31, 2015 at 12:34 PM
I hope this fits your needs. The Colors are normalized rgb values so they range between 0 and 1.
public Texture2D image;//Just assign your image in the inspector.
private GameObject CubeManager
Color GetColor(int x, int y) { //Get the color at a specific coordinate
x = Mathf.Clamp(x,0,image.width);
y = Mathf.Clamp(y,0,image.height);
return image.GetPixel(x,y);
}
void Start() {
CubeManager = new GameObject(); //So your hierachy doesn't get flooded you make them a child of the CubeManager gameobject.
CubeManager.name = "CubeManager";
GenerateMap();
}
void GenerateMap() {
for (int x = 0; x < image.width; x++) {
for (int y = 0; y < image.width; y++) {
GameObject cube = GameObject.CreatePrimitive(PrimitiveType.Cube);
cube.transform.parent = CubeManager.transform;
cube.renderer.material.color = GetColor(x,y);
if (GetColor(x,y) == Color.white) {
cube.transform.position = new Vector3(x,0,y);
} else {
cube.transform.position = new Vector3(x,1,y);
}
}
}
}
JS version:
public var image : Texture2D;//Just assign your image in the inspector.
private var CubeManager : GameObject;
function GetColor(x : int, y : int) { //Get the color at a specific coordinate
x = Mathf.Clamp(x,0,image.width);
y = Mathf.Clamp(y,0,image.height);
return image.GetPixel(x,y);
}
function Start() {
CubeManager = new GameObject(); //So your hierachy doesn't get flooded you make them a child of the CubeManager gameobject.
CubeManager.name = "CubeManager";
GenerateMap();
}
function GenerateMap() {
for (var x : int = 0; x < image.width; x++) {
for (var y : int = 0; y < image.width; y++) {
var cube : GameObject = GameObject.CreatePrimitive(PrimitiveType.Cube);
cube.transform.parent = CubeManager.transform;
cube.renderer.material.color = GetColor(x,y);
if (GetColor(x,y) == Color.white) {
cube.transform.position = new Vector3(x,0,y);
} else {
cube.transform.position = new Vector3(x,1,y);
}
}
}
}
Your answer
Follow this Question
Related Questions
Check if an Object is intersecting any other object 0 Answers
Unity Height Glitch (explained) 1 Answer
What's the best/easiest way to do something like Bounds.Intersects but accounting for rotation? 1 Answer
Simple example bound box or get the top position gameobject 1 Answer
Attaching a Hinge Joint upon colliding with a rigidbody. 1 Answer