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)];
}
}
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..
– GabboUnityYou 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);
– robertbuI tried, but it seems that nothing happens. How can i be able to check same colors between the two colliders (ball and plane)?
– GabboUnityvar 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..
– GabboUnityAs mentioned in my previous comment, it needs to be in Update(). Here is a complete script. #pragma strict function Update() { var hit : RaycastHit; if (Physics.Raycast(transform.position, Vector3.down, hit)) { Debug.Log(hit.collider.name +", " + hit.collider.tag); var rend = hit.collider.renderer; if (rend != null) { if (rend.material.color == renderer.material.color) { Debug.Log("Found a match"); } } } }
– robertbu