x


How do I Adjust The Colour Of My Object By Percentages?

Hello, and how are you?

I'm trying (and very, very hard at that!) to change the colour of an object by picking up other objects, what are a certain colour.

For example:

If I was to pick up two red objects and a blue object I would want my object to be 2 parts red and 1 part blue.

Here is the script i'm using.

var red : float; 
var green : float; 
var blue : float; 
var purple : float;
var orange : float;

var playerColor : Material;

function Start () {

   playerColor = renderer.material;

}

function Update () {

   red = red + purple / 2 + orange /3;

   green = green + orange /3;

   blue = blue + purple / 2 + orange /3;

   playerColor.SetColor("_Tint", Color(red, green, blue));

}

function OnCollisionEnter (other : Collision) {

if (other.transform.tag == "Particle") {

   other.gameObject.SendMessage("SendParticle");

   Destroy (other.gameObject);

}

}

function AddBlue () {

   blue ++;

}

function AddRed () {

   red ++;

}

function AddOrange () {

   orange ++;

}

function AddPurple () {

   purple ++;

}

function AddGreen () {

   green ++;

}

So yeah... It doesn't work. Maybe a tip or two (something...anything..) on how colour works?

Thanks!

more ▼

asked Apr 02 '12 at 04:20 PM

MithosAnnar gravatar image

MithosAnnar
299 22 36 41

(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

The Color object in Unity has 4 components r, g, b and a, all of them between 0 and 1, (1,1,1,1) being white. If you keep adding to your colors, it will inevitably end up white.

What you want is a mix between color. You should look at Color.Lerp. However, an interpolation done that way can give you black or dark color inbetween, and you probably don't want that. You should take a look at HSV colors (hue saturation value), that use a Lerp probably more relevant to your needs.

more ▼

answered Apr 02 '12 at 04:32 PM

Berenger gravatar image

Berenger
11.2k 12 19 54

@Bérenger Mantoue: Thanks for your reply. I am figuring it out this way as of current!

Apr 02 '12 at 05:44 PM MithosAnnar

It worked!

Thanks again!

Apr 03 '12 at 03:57 PM MithosAnnar
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x1123
x841
x526
x264
x19

asked: Apr 02 '12 at 04:20 PM

Seen: 638 times

Last Updated: Apr 03 '12 at 03:57 PM