- Home /
Changing the alpha of an Unlit/Transparent shader
Hey guys,
I'm working on a top-down 2D space sim, and I'm currently working on the graphic element that conveys motion when the spaceship is moving.
What I have is a quad, which moves with the ship, with a material on it that moves in the opposite direction that the ship is moving.
The quad follows the ship because of the following script:
public class cs_GridFollow : MonoBehaviour {
// define the grid's position on the Y-axis
public float Ypos = -1.0f;
// define player position for grid to follow
public Transform player;
void Update()
{
// grid follows player position
transform.position = player.position + new Vector3(0, Ypos, 0);
}
}
The texture on the quad moves in the opposite direction of the ship because of the this script:
public class cs_ScrollUV : MonoBehaviour {
// This is the dimension of the quad divided by 20, the number of times
// the texture is tiled across the material
public float UVscaleFactor;
private MeshRenderer mr;
private Material mat;
private Vector2 offset;
void Start()
{
// Calls the MeshRenderer component of this game object
mr = GetComponent<MeshRenderer>();
// Creates an instance for this game object's material
mat = mr.material;
// Creates the Vector2 instance that will tell the UVs which way to go
offset = mat.mainTextureOffset;
}
void Update()
{
// Defines the X and Y components of the Vector2
offset.x = transform.position.x / UVscaleFactor;
offset.y = transform.position.z / UVscaleFactor;
// The material's UVs are offset by the Vector2 "offset"
mat.mainTextureOffset = offset;
}
}
I want to map the opacity of this graphic to the ship's speed, so that it is only visible when the ship is moving, and the faster the ship moves, the higher the opacity of the starfield texture flying by.
I wrote the following script to attempt to accomplish this:
public class cs_VisibilityWithSpeed : MonoBehaviour
{
public float opacity;
private MeshRenderer mr;
private Material mat;
void Start ()
{
mr = GetComponent<MeshRenderer>(); // Calls the MeshRenderer component of this game object
mat = mr.material; // Creates an instance for this game object's material
}
void Update ()
{
mat.color = new Color(1f, 1f, 1f, opacity);
// NOTE: I haven't yet added code to link the opacity to the ship's motion yet.
// I'm still trying to get the texture to even recognize the opacity variable.
}
}
The problem I'm havng is that because of the type of shader I'm using(Unlit/Transparent), which is necessary for it to have transparency between the stars, there is apparently no control of the overall color, where the alpha setting is. And if I were to use a sprite, there would be no controls for the tiling and offset, which I need. So what can I do? There must be a solution. So many games have this type of effect.
So are you basically looking for a fade animation between black and transparent which simulates the movement of the ship? If so this tutorial might help you: https://www.youtube.com/watch?v=2XNP6Qf2gDU I know it isn't in the same context as your idea, but the same concept applies.
No, not exactly. The tutorial you linked has him animating the alpha, which is what I'm trying to do, but like I mentioned before, I'm not able to access the alpha with the shader type I'm using.
I currently have an image that has transparency in it. What I'm trying to do is make it completely transparent when it is not moving, but I don't want to disable it, I would like it to fade.
Your answer
Follow this Question
Related Questions
An Outline like the Editor does in game, with transparency 1 Answer
Does the Particle Animator work with transparent shaders? 1 Answer
Different transparency on same texture? 1 Answer
Texture transparency not working as expected 3 Answers
transparent RGBA PNG textures worked in 4.6, how can I get them working in Unity5? 1 Answer