- Home /
Check planes that have the same color of gameobject
Hi guys! In my game I have a sphere that rolls on some planes that are created and colored randomly and I want to check when the planes have got the same color of the ball (that is also colored randomly) because I want that when the ball is on a plane that has got its same color, the player must click on the screen to increase the score and to change the color of the sphere. Then, obviously, if the player doesn't click on the screen when the sphere rolls on the first plane that have its same color the game quit. How can I do? Now I write the script that I have to create random planes and to change the color of the sphere after touching screen. Thanks a lot!
SCRIPT TO CREATE RANDOM PLANES.
#pragma strict
import System.Collections.Generic;
var list : List. = new List.();
var s : GameObject;
var colors = [Color.red, Color.green, Color.blue, Color.yellow, Color.white];
function Start () { StartCoroutine(creazione(0.000000000001)); }
function creazione(tempo) {
while(true) {
var pos : Vector3;
for (var i = 0; i < 30; i++) {
pos = Vector3(0f,0f,Random.Range(0,100000));
if (!CheckOverlap(pos)) break;
}
if (CheckOverlap(pos)) break;
s = GameObject.CreatePrimitive(PrimitiveType.Plane);
s.renderer.material.color = colors[Random.Range(0, colors.Length)];
s.transform.position = pos;
list.Add(s.transform);
yield WaitForSeconds(tempo);
}
}
function CheckOverlap(pos : Vector3) {
for (var t : Transform in list) {
if (Vector3.Distance(t.position, pos) < 10.0)
return true;
}
return false;
}
SCRIPT TO CHANGE THE COLOR OF THE SPHERE.
var sphere : GameObject;
var colors = [Color.red, Color.green, Color.blue, Color.yellow, Color.white];
function Update() {
if (Input.touchCount == 1) {
sphere.renderer.material.color = colors[Random.Range(0, colors.Length)];
}
}
Answer by robertbu · Sep 27, 2014 at 03:16 PM
Here is a bit of untested code that Raycasts() down from the center of an object and find and tests the color against the current object:
var hit : RaycastHit;
if (Physics.Raycast(transform.position, Vector3.down, hit)) {
var rend = hit.collider.renderer;
if (rend != null) {
if (rend.material.color == renderer.material.color) {
// Do something
}
}
}
Note I use a direct comparison operator ('==') to test the two colors. I'm not sure how Unity handles comparisons of colors. If two colors that appear to be the same you can check with Debug.Log()) are not considered equal, you can check the color distance instead.
function ColorSqrDist(c1 : Color, c2: Color) : float {
return (c2.r - c1.r) * (c2.r - c1.r) + (c2.g - c1.g) * (c2.g - c1.g) + (c2.b - c1.b) * (c2.b - c1.b));
}
But this script works only with real collisions? I write this script: var hit : RaycastHit; var sphere : GameObject; var colors = [Color.red, Color.green, Color.blue, Color.yellow, Color.white];
if (Physics.Raycast(transform.position, Vector3.down, hit)) { var rend = hit.collider.renderer; if (rend != null) { if (rend.material.color == renderer.material.color) { sphere.renderer.material.color = colors[Random.Range(0, colors.Length)]; } } } and I attached it on the sphere, but nothing happens..
You need to debug it. It will only work if the planes have a mesh collider, and if the first object below your ball is the plane. Put a Debug.Log() statement just inside the 'if' statement to see if it is hitting something, and if so what:
Debug.Log(hit.collider.name + ", " + hit.collider.tag);
If it is hitting something, then just before doing the comparison, do:
Debug.Log(rend.material.color + ", " + renderer.material.color);
I tried, but it seems that nothing happens. How can i be able to check same colors between the two colliders (ball and plane)?
The above fragment goes in the Update() function of a script on the ball. Given a hit by the Raycast(), this:
var rend = hit.collider.renderer;
...gets the renderer on the object hit and this:
rend.material.color
...get the color. If you are absolutely sure that what get hit has a Renderer, you can do it in one step:
var color = hit.collider.renderer.material.color;
I tried, but it seems that nothing happens.
First if I did not make it clear in may answer, the above is a code fragment that needs to go in Update(). That is, the Raycast() needs to happen each frame. If it is in Update(), then please post your current entire script on the ball, and I'll take a look to see what is going on.
var hit : RaycastHit;
if (Physics.Raycast(transform.position, Vector3.down, hit)) { var rend = hit.collider.renderer; if (rend != null) { if (rend.material.color == renderer.material.color) { Debug.Log(hit.collider.name + ", " + hit.collider.tag); } } }
This is the script that I attached on my sphere..
Answer by GabboUnity · Sep 29, 2014 at 12:58 PM
I wrote the new answer @robertbu at this URL: http://answers.unity3d.com/questions/799643/check-touch-on-mobile-screen-and-do-different-thin.html Look at it if you have anytime!
Your answer
Follow this Question
Related Questions
Check for closed circuit of pickups 2 Answers
How to check if specific objects touch 0 Answers
How can I check if my Player leaves a Sphere? 2 Answers
Android touch is not dynamic 1 Answer