I would like to know how to change player color by code.

0
Thanitsak posted this 06 May 2017

I would like to change player color by code when it get hit and that should be done in code so is there any way to do that? Thank you very much!

11 Comments
Order By: Standard | Newest | Votes
1
GarethIW posted this 06 May 2017

It might be enough to change the Tint colour of the material assigned to your volume. As Tint is a shader parameter, you would do this in the usual Unity way by getting a reference to the Volume's material and setting the _Tint parameter:

var mat = GetComponent<meshrenderer>().material;
mat.SetColor("_Tint", Color.red);

However, if you're looking to do the usual thing of making the model flash bright white for instance, just setting the tint to white won't be enough, as the default tint is already white (the tint is actually just a colour multiplied by the voxel colour). The answer here would be to write a shader to mix between plain white and the voxel's vertex colour based on a 0-1 value. I might see if I can knock something up for you - I'm on a boat right now so this is a train-of-thought reply :)

Another option is to have an all-white version of the model that uses the PicaVoxel transparent shader, and have it overlaid on your model with a slightly larger size (like .001 larger to avoid z-fighting), then change the transparent alpha using code similar to the above.

So yeah, lots of way you can do it! I'll see if I can shader it for you when I get 10 minutes.

1
GarethIW posted this 06 May 2017

Alright, here's a new shader based on the base PicaVoxel PBR one. I've also included a material so all you have to do is swap the material on your player PicaVoxel Volume with the included PicaVoxel PBR with Fader material.

[See below]

Once you have done that, if you open the material in the editor, you'll notice it has two new parameters: Fade Color and a Fade Amount slider. If you slide the slider toward 1, you will notice the volume's colours fade to all white, or whatever you set the Fade Colour to.

In code, you can change the Fade Amount with similar code to my above post:

var mat = GetComponent<meshrenderer>().material;
mat.SetFloat("_FadeAmount", 1f);

Hope that helps!

Last edited 14 May 2017

0
Thanitsak posted this 07 May 2017

Wow! It work like a charm! Much much much appreciated!!! :D

0
Thanitsak posted this 07 May 2017

Oh when I build my game to iOS the material isn't work, I mean the color is not fade. But it work fine when I test the game in unity. Much appreciate!

1
GarethIW posted this 07 May 2017

Sorry, but I don't currently have a setup for iOS deployment (I don't have a dev-activated device and my subscription lapsed). If there are no shader errors in the editor or in the developer console on the device then I can't help.

There's nothing special about the changes I made to the shader, but as I use ShaderForge I don't have direct control over the code. If you have ShaderForge then you can play about with the shader properties and try to get it working on the device.

You may have to look at the other option I suggested in my original reply. Have a white-only version of the model overlaid, and set it up with the transparent shader, and change the alpha value from 0 to 1 and back again.

1
GarethIW posted this 07 May 2017

Hmmm, I just had another thought. I've messed about with some of the build setting on that shader, and I've also made a version of the Diffuse shader with the fader as well. If the updated PBR one doesn't work, try the Diffuse one.

[See below]

Last edited 14 May 2017

0
Thanitsak posted this 09 May 2017

Thank You Very Much for the new files, but after I downloaded it said not things to import.

Much Appreciate!

1
GarethIW posted this 09 May 2017

Okay, here's a zip instead: [See below]

Last edited 14 May 2017

0
Thanitsak posted this 13 May 2017

Nothing happen when I clicked into the link. :)

1
GarethIW posted this 14 May 2017

Sorry about that, should have worked. I've re-shared it and tested in incognito mode:

https://1drv.ms/u/s!AhdchalVsPvXnL8SvXmeNOYXLjDT7Q

0
Thanitsak posted this 15 May 2017

WOW!!! PBR with fader work!!!

Thank You Very Much and Much Much Appreciate!!!


Our 103 members have posted 431 times in 104 discussions