- Home /
How to make a game object react to a colour
i want this to happen without collision bounds. i know it is possible, because BYOB can do it (search on google to find BYOB) i want a ball to freeze when it touchs the colour black (oncollisionenter if colour is black) / only move on the colour white. (if colour is white then move.......)
i want it in this script:
var speed = 1;
function Update () {
var dir : Vector3 = Vector3.zero;
dir.x = (Input.acceleration.x*.5/50)*speed;
transform.position.x += dir.x;
dir.y = (Input.acceleration.y*.5/50)*speed;
transform.position.y += dir.y;
}
function OnCollisionEnter(collision : Collision) {
speed=0;
}
Thanks!
Answer by AlwaysSunny · Apr 03, 2015 at 05:30 PM
Let's be clear: Reacting to what color? Whose color?
If you want to detect an object's renderer's material's "_MainColor" or "_TintColor" - that's easy. If you want to include a small margin for error there, that's likewise easy.
However, if you want to detect the final rendered color of a specific point of a textured, lit environment; that becomes quite challenging and expensive.
Based on your question, I assume you want the former, easy case. In which case, the following will get you the "other" object's renderer's material's _MainColor.
(My unityscript is very rusty, plz allow for errors)
var color : Color = collision.gameObject.renderer.materials[0].GetColor("_MainColor");
Comparing one color to another can be accomplished via a simple equality check; allowing for a margin of error can be accomplished by comparing the RGBA components of that color to your test sample, allowing for slight differences.