Search Unity

  1. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice
  2. Ever participated in one our Game Jams? Want pointers on your project? Our Evangelists will be available on Friday to give feedback. Come share your games with us!
    Dismiss Notice

FogOfWar Dynamic Shader Based By Sergey Taraban[2D application Problem Please Help Me]

Discussion in '2D' started by Mallow.Mellow, Mar 4, 2015.

  1. Mallow.Mellow

    Mallow.Mellow

    Joined:
    Mar 4, 2015
    Posts:
    15
    uhm...I'm Having a Trouble right now and I can't Find the Problem Because I'm really a noob here but I want to Learn. Please Help Me :) Please!

    Okay...The Only problem is the Spotlight thing in the shader doesn't follow my character or player whenever it moves away...
    wewew.PNG

    as you can see the spotlight or the hole doesn't follow my character...
    How can I fix this?
    I Follow all the steps of Sergey Taraban but I change a little bit the codes in my shader of course because its 2D not 3D like in Sergey Taraban's Video on Youtube...

    So this is how it looks like the Codes in my FogOfWar.shader:


    Code (CSharp):
    1. Shader "Custom/FogOfWar" {
    2.     Properties {
    3.         _Color ("Main Color", Color) = (1,1,1,1)
    4.         _MainTex ("Base (RGB)", 2D) = "white" {}
    5.         _FogRadius ("FogRadius", Float) = 1.0
    6.         _FogMaxRadius ("FogMaxRadius", Float) = 0.5
    7.         _Player_Pos ("_Player_Pos", Vector) = (-1.19,-0.05,0,1)
    8.  
    9.  
    10.     }
    11.     SubShader {
    12.         Tags { "Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Opaque" }
    13.         LOD 200
    14.         Blend SrcAlpha OneMinusSrcAlpha
    15.         Cull Off
    16.  
    17.         CGPROGRAM
    18.         #pragma surface surf Lambert vertex:vert
    19.  
    20.         sampler2D _MainTex;
    21.         fixed4 _Color;
    22.         float  _FogRadius;
    23.         float  _FogMaxRadius;
    24.         float4 _Player_Pos;
    25.  
    26.         struct Input {
    27.             float2 uv_MainTex;
    28.             float2 location;
    29.      
    30.         };
    31.         float powerForPos(float4 pos, float2 nearVertex);
    32.  
    33.         void vert(inout appdata_full vertexData, out Input outData){
    34.             float4 pos = mul(UNITY_MATRIX_MVP, vertexData.vertex);
    35.             float4 posWorld = mul(_Object2World, vertexData.vertex);
    36.             outData.uv_MainTex = vertexData.texcoord;
    37.             outData.location = posWorld.xy;
    38.         }
    39.  
    40.         void surf (Input IN, inout SurfaceOutput o) {
    41.             fixed4 baseColor = tex2D (_MainTex, IN.uv_MainTex) * _Color;
    42.      
    43.             float alpha = (1.0 - powerForPos(_Player_Pos, IN.location));
    44.      
    45.             o.Albedo = baseColor.rgb;
    46.             o.Alpha = alpha;
    47.         }
    48.         //return 0 if (pos - nearVertex) > FogRadius
    49.         float powerForPos(float4 pos, float2 nearVertex){
    50.         float atten = (_FogRadius - length(pos.xy - nearVertex.xy));
    51.         return (1.0/_FogMaxRadius)*atten/_FogRadius;
    52.         }
    53.  
    54.         ENDCG
    55.     }
    56.     FallBack "Transparent/VertixLit"
    57. }
    58.  
    And My Code in FogOfWarPlayer.CS:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class FogOfWarPlayer : MonoBehaviour {
    5.  
    6.     public Transform FogOfWar;
    7.     public int Number = 1;
    8.  
    9.     // Use this for initialization
    10.     void Start () {
    11.     }
    12.  
    13.     // Update is called once per frame
    14.     void Update () {
    15.  
    16.         Vector3 screenPos = Camera.mainCamera.WorldToScreenPoint(transform.position);
    17.         Ray rayToPlayerPos = Camera.mainCamera.ScreenPointToRay(screenPos);
    18.         int layermask = (int)(1<<8);
    19.         RaycastHit hit;
    20.         if (Physics.Raycast (rayToPlayerPos, out hit, 10000, layermask)) {
    21.  
    22.                 FogOfWar.GetComponent<Renderer> ().material.SetVector ("_Player_Pos", hit.point);
    23.          
    24.         }
    25.     }
    26.  
    27. }
    28.  
     
    Last edited: Mar 4, 2015
  2. Mallow.Mellow

    Mallow.Mellow

    Joined:
    Mar 4, 2015
    Posts:
    15
    Come On Guys...Please I need Help :(
    I did several things already...
    but I really cant Find out Why the Spotlight thing won't follow my character..
     
  3. Mallow.Mellow

    Mallow.Mellow

    Joined:
    Mar 4, 2015
    Posts:
    15
  4. TehReason

    TehReason

    Joined:
    May 14, 2013
    Posts:
    53
    probably because this part doesnt gets called:

    Code (CSharp):
    1. if (Physics.Raycast (rayToPlayerPos, out hit, 10000, layermask)) {
    2.                 FogOfWar.GetComponent<Renderer> ().material.SetVector ("_Player_Pos", hit.point);
    3.        
    4.         }
    Check the condition through a debug message or something alike
     
    Mallow.Mellow likes this.
  5. Mallow.Mellow

    Mallow.Mellow

    Joined:
    Mar 4, 2015
    Posts:
    15
    I tried print("anything");
    inside the if statement...
    and there's no output...
    do you know any other condition that will work to become alternate to this code or any logic?
    like I will get the components of my shader here:
    reply.PNG
    and every time the player moves(everyFPS) the _Player_Pos x and y values(P.S.) will gonna be the same as the value of the player/character Transform x and y values?
     
  6. Mallow.Mellow

    Mallow.Mellow

    Joined:
    Mar 4, 2015
    Posts:
    15
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class FogOfWarPlayer : MonoBehaviour {
    5.  
    6.     public Transform FogOfWarPlane, SightStart, SightEnd;
    7.  
    8.     // Use this for initialization
    9.     void Start () {
    10.    
    11.     }
    12.    
    13.     // Update is called once per frame  
    14.     public void Update () {
    15.         Raycasting ();
    16.  
    17.         Vector3 ScreenPos = Camera.main.WorldToScreenPoint(transform.position);
    18.    
    19.         Ray rayToPlayerPos = Camera.main.ScreenPointToRay(ScreenPos);
    20.         Debug.DrawRay(rayToPlayerPos.origin, rayToPlayerPos.direction * 10, Color.red);
    21.  
    22.         int layermask = (int)(1<<8);
    23.  
    24.         RaycastHit hit;
    25.         if (Physics.Raycast (rayToPlayerPos,out hit , 1000, layermask))
    26.         {
    27.             print("Testing");
    28.             FogOfWarPlane.GetComponent<Renderer>().material.SetVector ("_Player_Pos", hit.point);
    29.         }
    30.  
    31.     }
    32.     void Raycasting () {
    33.         Debug.DrawLine (SightStart.position, SightEnd.position, Color.green);
    34.  
    35.  
    36.     }
    37.  
    38.  
    39. }
    This is the whole code in My FogOfWarPlayer rightnow
     
  7. Mallow.Mellow

    Mallow.Mellow

    Joined:
    Mar 4, 2015
    Posts:
    15
    I'm confused in the FogOfWarPlayer.cs
    because I tried to rewrite the other codes like Vector3 to Vector2, Ray to Ray2D, RaycastHit to RaycastHIt2D, Physics to Physics2D and the 1000 to different numbers like 1000000 or 1.5f and also the Camera.mainCamera.blahblah to Camera.main.WorldToScreenPoint ahahahaha
     
  8. TehReason

    TehReason

    Joined:
    May 14, 2013
    Posts:
    53
    why do you even need that check? And whats on your layer 8?
     
  9. Mallow.Mellow

    Mallow.Mellow

    Joined:
    Mar 4, 2015
    Posts:
    15
    uhmm...
    nothing.
    I'm just experimenting because I don't fully understand the code.
    But Sergey said:" Yes it is applyable but if you work in Unity 2D mode you need to rewrite shader because Unity2D mode uses other coordinate system."

    I modify it a little bit that's why its circle now and not like this:
    reply2.PNG

    to be honest this is my first time using a shader,
    and I'm not really good also in C# that's why I'm here.
    I want to learn from my mistakes.
    and sorry for my English :(
     
  10. Mallow.Mellow

    Mallow.Mellow

    Joined:
    Mar 4, 2015
    Posts:
    15
  11. mmortall

    mmortall

    Joined:
    Dec 28, 2010
    Posts:
    89
    Mallow.Mellow please send me your project or part of it I will try to solve the problem
     
    Mallow.Mellow likes this.
  12. Mallow.Mellow

    Mallow.Mellow

    Joined:
    Mar 4, 2015
    Posts:
    15
    How can I send it to you?
    via?
     
unityunity