Is there a way to make an object half opaque? I tried this:
renderer.material.color = new Color(255f, 255f, 255f, 0.5f);
But it just makes the item white and the textures are no longer showing.
I have tried using Transparent/Bumped Specular but that makes the object too transparent, even when I don't want the object transparent at the time.
What I am trying to accomplish is to make it transparent to look like the player is inactive during a character selection screen. When the player presses A they will go to full opaque and then can select their player.
Editor Mode:
Play Mode:
So there are a couple things that I'd like to point out to you on this. First off, Unity's Color class is based on values from 0 to 1. So you have 255f in your RGB's, this is giving you what you expect now, but really it's just setting them to 1 in the constructor. So if you want colors in the future do:
new Color(rValue/255f, gValue/255f, bValue/255f, aValue/255f);
Just wanted to point that out while you were here.
So to your actual question, what you're doing with that line of code is you're saying, take the material attached to this object and make it completely white (if i'm wrong with 1,1,1 being white, someone please comment) and half transparent. But that doesn't apply to the object itself, that only applies to the material attached.
So if you want to modify the platform to be transparent, my recommendation is store both the transparent and non-transparent materials that you want on the script where you're changing which one is being used. Then in the code, instead of changing colors, change what material the renderer is using instead.
Hope this helps.
Related
I have a first person controller and currently if I walk into a slightly transparent object, it disappears until I walk out of it.
I have a 'water' cube object that is very light blue and transparent, and when I move my camera into third person view then enter the water, my screen turns light blue which is good. In first person mode (which is what I'm trying to figure out), the cube disappears and my screen color remains the same.
I know it has something to do with my camera but even after going through all the features on unity docs and changing a few settings in the inspector, it all remains the same.
Like mentioned in the comments your cube is not visible because the backside of its faces is being culled, meaning they are not rendered. This is not a camera setting but a property of the shader your water cubes material is using.
You could create get a duplicate of the shader your are using where you add Cull Off to change this.
Read about it here.
If you want to go that route you need the source files for your shader. Assuming it is one of the Unity Built in shaders you can get them from the Unity Download archive here for your Unity version.
Clipping the camera trough objects is rarely a desired and you should look at a different way of achieving your effect like using a post-processing volume because the "water" would still be a cube and be drawn behind everything in front of its faces.
For example in a FPS type game this would result in the gun not changing color.
As you can see in my Unity scene
it looks dark.
game tab
you can see here that it's dark.
I've done going to window > render > lightning > clear baked data but its still the same.
Any unity devs can help me, thanks
As you can see in my lightning tab
auto generate is disabled so I can't check it to verify if it can solve my problem.
Somewhere in the Canvas you have an object that has a sprite on it where the color is modified. Also check the alpha setting for the colors there.
Lighting doesn't affect UI elements like that. That only applies to non UIElements like 3D and 2D objects placed in the world (https://docs.unity3d.com/Manual/LightingInUnity.html).
So it's just a sprite that is causing this background color as #Çağatay IŞIK suggested.
I have a game in Unity.
The idea of the game is that an object is spawned on the screen.
It exists for several seconds.
You need to click on it, while it exists.
What I want to do, is that while it exists - I want to show a timer, but not with numbers, but with an outlined line on the perimeter of the screen.
I know, how to make a make a slider and how to make circular health bar, yet I do not really have an idea to make a slider, that is decresing from everywhere to the centre as you can see on the screenshots below.
I would be thankful for the inspiration!
This should be a very good fit using alpha cutoff with a custom shader.
Here's a video explaining it in detail (it's only the first half you will need).
The basic idea is to create a texture with a graded alpha depending on how you want the texture to appear/disappear. In this case the alpha value at the top and the bottom would be close to 0 and then gradually around the line on both sides towards the middle increase to 1. The shader then cuts off the texture below a cutoff threshold which you can change depending on the value of the timer.
As a dumb and simple approach:
Have two images with fill, one goes middle towards top the other middle towards bottom.
Just imagine the pnguin is your outline ^^
So I'm trying to add an effect to a game object where the transparency of its materials will grow over time until it hits a threshold, then go back to normal, and repeat for a predetermined amount of time. I made it so I can make them transparent after a bit of research, but my issue is now just trying to use the best transparent shader.
I first used the UI/Unlit/Transparent shader where I could just grab the alpha value and increment/decrement it as I wanted and made it go transparent, but my issue was that the tint of that material would make it blindingly white, which I don't know if I can do anything about since I am using PNGs as the material so I can't just edit the tint to not affect the material I think.
Then I used the Unlit/Transparent shader but that only has _MainText or Diffuse, which doesn't have a way to edit the transparency without a custom shader is what I've found after scrolling through Unity Answers and other StackOverflow posts.
Is my only option to write a custom shader for Diffuse to get this to work or is there something obvious that I'm missing for these shaders?
Alright I figured out that all I had to do was check off the "Use Alpha Clip" option on the UI/Unlit/Transparent shader and it only uses the alpha variable and not the tint
So I'm looking to create an effect of having a bubble around my player which, when he enters a hidden area (hidden by tilemaps) the bubble activates and it essentially has an xray effect. So I can see the background, the ground and all the items inside the area I just can't see the blocks themselves.
So pretty much going from this
To this
And as I go further in the more gets revealed
I have no idea what to even begin searching for this. So any direction would be greatly appreciated
First of all, I want to get something out of the way: Making things appear when they are nearby the player is easy, you use a light and a shader. Making things disappear when they are nearby the player by that approach is impossible in 2D (3D has flags_use_shadow_to_opacity).
This is the plan: We are going to create a texture that will work as mask for what to show and what not to show. Then we will use that texture mask with a shader to make a material that selectively disappears. To create that texture, we are going to use a Viewport, so we can get a ViewportTexture from it.
The Viewport setup is like this:
Viewport
├ ColorRect
└ Sprite
Set the Viewport with the following properties:
Size: give it the window size (the default is 1024 by 600)
Hdr: disable
Disable 3D: enable
Usage: 2D
Update mode: Always
For the Sprite you want a grayscale texture, perhaps with transparency. It will be the shape you want to reveal around the player.
And for the ColorRect you want to set the background color as either black or white. Whatever is the opposite of the color on the Sprite.
Next, you are going to attach a script to the Viewport. It has to deal with two concerns:
Move the Sprite to match the position of the player. That looks like this:
extends Viewport
export var target_path:NodePath
func _process(_delta:float) -> void:
var target := get_node_or_null(target_path) as Node2D
if target == null:
return
$Sprite.position = target.get_viewport().get_canvas_transform().origin
And you are going to set the target_path to reference the player avatar.
In this code target.get_viewport().get_canvas_transform().origin will give us the position of the target node (the player avatar) on the screen. And we are placing the Sprite to match.
Handle window resizes. That looks like this:
func _ready():
# warning-ignore:return_value_discarded
get_tree().get_root().connect("size_changed", self, "_on_size_changed")
func _on_size_changed():
size = get_tree().get_root().size
In this code we connect to the "size_changed" of the root Viewport (the one associated with the Window), and change the size of this Viewport to match.
The next thing is the shader. Go to your TileMap or whatever you want to make disappear and add a shader material. This is the code for it:
shader_type canvas_item;
uniform sampler2D mask;
void fragment()
{
COLOR.rgb = texture(TEXTURE, UV).rgb;
COLOR.a = texture(mask, SCREEN_UV).r;
}
As you can see, the first line will be setting the red, green, and blue channels to match the texture the node already has. But the alpha channel will be set to one of the channels (the red one in this case) of the mask texture.
Note: The above code will make whatever is in the black parts fully invisible, and whatever is in the white parts fully visible. If you want to invert that, change COLOR.a = texture(mask, SCREEN_UV).r; to COLOR.a = 1.0 - texture(mask, SCREEN_UV).r;.
We, of course, need to set that mask texture. After you set that code, there should be a shader param under the shader material called "Mask", set it to a new ViewportTexture and set the Viewport to the one we set before.
And we are done.
I tested this with this texture from publicdomainvectors.org:
Plus some tiles from Kenney. They are all, of course, under public domain.
This is how it looks like:
Experiment with different textures for different results. Also, you can add a shader to the Sprite for extra effect. For example add some ripples, by giving a shader material to the Sprite with code like this one:
shader_type canvas_item;
void fragment()
{
float width = SCREEN_PIXEL_SIZE.x * 16.0;
COLOR = texture(TEXTURE, vec2(UV.x + sin(UV.y * 32.0 + TIME * 2.0) * width, UV.y));
}
So you get this result:
There is an instant when the above animation stutters. That is because I didn't cut the loop perfectly. Not an issue in game. Also the animation has much less frames per second than the game would.
Addendum A couple things I want to add:
You can create a texture by other means. I have a couple other answer where I cover some of it
How can I bake 2D sprites in Godot at runtime? where we use blit_rect. You might also be interested in blit_rect_mask.
Godot repeating breaks script where we are using lockbits.
I wrote a shader that outputs on the alpha channel here. Other options include:
Using BackBufferCopy.
To discard fragments.