- Home /
Make and object invisible while in motion? (3d)
Hey guys! I am working on a concept in which objects become invisible while the player is in motion and are visible while standing still. I still want the rigidbody and collider to be active while the object is invisible. Does anyone know how to create something like this?
Thanks!
Answer by Eno-Khaon · Sep 25, 2017 at 01:59 AM
One way to approach this would be using shaders attached to those objects. You could make a shader which supports transparency, then modify that value using something like:
int movementMultiplier = (playerRigidbody.velocity.sqrMagnitude > 0.001f) ? 0 : 1;
Shader.SetGlobalFloat("_PlayerMoving", movementMultiplier);
Then, in your shaders, define and use:
float _PlayerMoving;
// ...
finalColor.a = saturate(_PlayerMoving);
I know this answer isn't especially comprehensive and thorough, but hopefully this may provide some food for thought.
Thanks for the reply. I’ll try it in the morning but I was hoping for a more scripted approach (I feel more comfortable with code and I like to easily tweak values). Thanks again!
Answer by Kishotta · Sep 25, 2017 at 04:12 AM
Could you just disable the MeshRenderer component?
if (isMoving) { // however you want to determine this
GetComponent<MeshRenderer> ().enabled = false; // could cache MeshRenderer for performance
} else {
GetComponent<MeshRenderer> ().enabled = true;
}
Yeah that should work. I’ll try it tomorrow and see what happens. I also just thought of another idea - I could create two identical objects (one with a meshrenderer and one without) and I could disable the one with the renderer while moving (urs is more simple tho)