Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Help with a function

Discussion in 'Scripting' started by hecks, Jun 7, 2016.

  1. hecks

    hecks

    Joined:
    Jun 6, 2016
    Posts:
    2
    hello

    I'm a new user of unity , i was watching somes videos about unity and i like it , so i wanted to try.
    I'm not new at all about scripting im starting to learn java .
    well my problem is this

    Im making a fan made of "slenderman" im just to practice scripting and some easy stuff but i needed a script of slenderman games vercion and that one is the static screen.
    so i was looking in forums and and other sites and i found a script that works hm 80% or so i was trying to fix it
    so here is it
    Code (JavaScript):
    1. #pragma strict
    2. @script RequireComponent(MeshFilter, MeshRenderer)
    3. var theAlpha : float = 0.0;
    4. var theCamera : Camera;
    5. var cameraTransform : Transform;
    6. private var mesh : Mesh;
    7. private var uv : Vector2[];
    8. private var verts : Vector3[];
    9. private var tris : int[];
    10. private var normals : Vector3[];
    11. public var distance : float = 1.0;
    12. private var theMaterial : Material;
    13. var theEnemy : EnemyScript;
    14. function Start()
    15. {
    16.     Startup();
    17.     // find and store a reference to the enemy script (to use health as alpha for texture)
    18.     if ( theEnemy == null )
    19.     {
    20.        theEnemy = GameObject.Find( "Enemy" ).GetComponent( EnemyScript );
    21.     }
    22. }
    23. function Update()
    24. {
    25.     SetAlpha();
    26.     ScrollUVs();
    27. }
    28. function SetAlpha()
    29. {
    30.     theAlpha = ( 100.0 - theEnemy.health ) * 0.01;
    31.     theMaterial.color = Color( theMaterial.color.r, theMaterial.color.g, theMaterial.color.b, theAlpha );
    32. }
    33. function ScrollUVs()
    34. {
    35.     var scrollX : float = Random.Range( -0.5, 0.5 );
    36.     var scrollY : float = Random.Range( -0.5, 0.5 );
    37.     // UVs
    38.     for ( var i:int = 0; i < 4; i ++ )
    39.     {
    40.        uv[i] = new Vector2( uv[i].x + scrollX, uv[i].y + scrollY );
    41.     }
    42.     mesh.uv = uv;
    43. }
    44. // ----
    45. function Startup()
    46. {
    47.     if ( theCamera == null )
    48.     {
    49.        theCamera = Camera.main;
    50.     }
    51.     cameraTransform = theCamera.transform;
    52.     theMaterial = gameObject.renderer.material;
    53.     theMaterial.color = Color.white;
    54.     if ( !mesh )
    55.     {
    56.        GetComponent(MeshFilter).mesh = mesh = new Mesh();
    57.        mesh.name = "ScreenMesh";
    58.     }
    59.     Construct();
    60.     //DebugVerts();
    61. }
    62. function Construct()
    63. {
    64.     mesh.Clear();
    65.     verts = new Vector3[4];
    66.     uv = new Vector2[4];
    67.     tris = new int[6];
    68.     normals = new Vector3[4];
    69.     // calculate verts based on camera FOV
    70.     var pos : Vector3 = cameraTransform.position - transform.position;
    71.     var halfFOV : float = ( theCamera.fieldOfView * 0.5 ) * Mathf.Deg2Rad;
    72.     var aspect : float = theCamera.aspect;
    73.     //Debug.Log( " Screen.width " + Screen.width + " : Screen.height " + Screen.height + " : aspect " + aspect );
    74.     var height : float = distance * Mathf.Tan( halfFOV );
    75.     var width : float = height * aspect;
    76.     //Debug.Log( " fieldOfView " + theCamera.fieldOfView + " : aspect " + aspect );
    77.     // UpperLeft
    78.     verts[0] = pos - (cameraTransform.right * width);
    79.     verts[0] += cameraTransform.up * height;
    80.     verts[0] += cameraTransform.forward * distance;
    81.     // UpperRight
    82.     verts[1] = pos + (cameraTransform.right * width);
    83.     verts[1] += cameraTransform.up * height;
    84.     verts[1] += cameraTransform.forward * distance;
    85.     // LowerLeft
    86.     verts[2] = pos - (cameraTransform.right * width);
    87.     verts[2] -= cameraTransform.up * height;
    88.     verts[2] += cameraTransform.forward * distance;
    89.     // LowerRight
    90.     verts[3] = pos + (cameraTransform.right * width);
    91.     verts[3] -= cameraTransform.up * height;
    92.     verts[3] += cameraTransform.forward * distance;
    93.     // UVs
    94.     uv[0] = new Vector2( 0.0, 1.0 );
    95.     uv[1] = new Vector2( 1.0, 1.0 );
    96.     uv[2] = new Vector2( 0.0, 0.0 );
    97.     uv[3] = new Vector2( 1.0, 0.0 );
    98.     // Triangles
    99.     tris[0] = 0;
    100.     tris[1] = 1;
    101.     tris[2] = 2;
    102.     tris[3] = 2;
    103.     tris[4] = 1;
    104.     tris[5] = 3;
    105.     // Normals
    106.     normals[0] = -Vector3.forward;
    107.     normals[1] = -Vector3.forward;
    108.     normals[2] = -Vector3.forward;
    109.     normals[3] = -Vector3.forward;
    110.     // assign mesh
    111.     mesh.vertices = verts;
    112.     mesh.uv = uv;
    113.     mesh.triangles = tris;
    114.     mesh.normals = normals;
    115.     mesh.RecalculateBounds();
    116.     mesh.RecalculateNormals();
    117. }
    118. /*
    119. function DebugVerts()
    120. {  
    121.     // Debug Positions
    122.     Debug.Log( " UL " + verts[0] + " : UR " + verts[1] );
    123.     Debug.Log( " LL " + verts[2] + " : LR " + verts[3] );
    124. }
    125. */


    so what's the problem my problem is that whenever the fps stay near of my "enemy" this stactic screen appears but when i run away this static screen doesn't disappear.
    and it's cuz the "alpha" doesn't reset whenever the fps isn't near of the enemy .
    i was trying some ways to fix it by myself watching and looking for something in the forum but i couldn't
    so i hope someone here can help me , the function i mean is this :


    Code (JavaScript):
    1. function SetAlpha()
    2. {
    3.     theAlpha = ( 100.0 - theEnemy.health ) * 0.01;
    4.     theMaterial.color = Color( theMaterial.color.r, theMaterial.color.g, theMaterial.color.b, theAlpha );
    5. }


    so as how the alpha doesn't reset the static screen doesn't disappear .

    script credits : (alucardj)
     
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,139
    I want to say that when you set theMaterial.color = Color, you must do theMaterial.color = new Color instead.
     
  3. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    Not in UnityScript.

    You said you wanted to remove the screen when the enemy wasn't near but I don't see anything that deals with distance in SetAlpha().
     
  4. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,139
    Ok, I know in c# it requires new for Color.

    One thing I see, is the script seems to be basing alpha off the enemy.health. However, if their health is at 100(assuming that is the max value). Your formula would be (100-100) * .01, which would return 0; 0 alpha means you're not going to see the image. Only as the health goes down does the image come into view. If the enemy "dies", and assuming you aren't resetting the value, your image will be in full view.
     
  5. hecks

    hecks

    Joined:
    Jun 6, 2016
    Posts:
    2
    i already tried that and that doesn't work that's why i don't understand it
     
  6. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,139
    Already tried what? Perhaps you need to explain your scenario better. You're saying something about staying near an enemy and running away, but your code doesn't do anything with distance like KelsoMRK said. Your setAlpha() is called from update, which means each frame it's getting the enemy health and doing some math and using that value for the alpha. But unless the health of the enemy changes somewhere based on distance from the player, your alpha value is based on a completely different value.