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

JS too C# conversion

Discussion in 'Scripting' started by IamGawd, Jun 8, 2014.

  1. IamGawd

    IamGawd

    Joined:
    Jun 7, 2014
    Posts:
    21
    Hello,

    I tried to convert this script to c# but I am finding errors can someone help?

    Code (JavaScript):
    1. // Blend the two terrain textures according to the steepness of
    2.     // the slope at each point.
    3.     function Start () {
    4.         var map: float[,,] = new float[t.terrainData.alphamapWidth, t.terrainData.alphamapHeight, 2];
    5.        
    6.         // For each point on the alphamap...
    7.         for (var y = 0; y < t.terrainData.alphamapHeight; y++) {
    8.             for (var x = 0; x < t.terrainData.alphamapWidth; x++) {
    9.                 // Get the normalized terrain coordinate that
    10.                 // corresponds to the the point.
    11.                 var normX = x * 1.0 / (t.terrainData.alphamapWidth - 1);
    12.                 var normY = y * 1.0 / (t.terrainData.alphamapHeight - 1);
    13.                
    14.                 // Get the steepness value at the normalized coordinate.
    15.                 var angle = t.terrainData.GetSteepness(normX, normY);
    16.                
    17.                 // Steepness is given as an angle, 0..90 degrees. Divide
    18.                 // by 90 to get an alpha blending value in the range 0..1.
    19.                 var frac = angle / 90.0;
    20.                 map[x, y, 0] = frac;
    21.                 map[x, y, 1] = 1 - frac;
    22.             }
    23.         }
    24.        
    25.         t.terrainData.SetAlphamaps(0, 0, map);
    26.     }
    My attempt..... and this is errors: Cannot implicitly convert type `float' to `int'. On lines (28,37) (28,40) (29,37) (29,40)

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Test : MonoBehaviour {
    5.  
    6.     private TerrainData terrainData;
    7.     private Terrain t;
    8.  
    9.     // Blend the two terrain textures according to the steepness of
    10.     // the slope at each point.
    11.     void Start () {
    12.         float[,,] map = new float[t.terrainData.alphamapWidth, t.terrainData.alphamapHeight, 2];
    13.        
    14.         // For each point on the alphamap...
    15.         for (float y = 0; y < t.terrainData.alphamapHeight; y++) {
    16.             for (float x = 0; x < t.terrainData.alphamapWidth; x++) {
    17.                 // Get the normalized terrain coordinate that
    18.                 // corresponds to the the point.
    19.                 float normX = x * 1.0f / (t.terrainData.alphamapWidth - 1);
    20.                 float normY = y * 1.0f / (t.terrainData.alphamapHeight - 1);
    21.                
    22.                 // Get the steepness value at the normalized coordinate.
    23.                 float angle = t.terrainData.GetSteepness(normX, normY);
    24.                
    25.                 // Steepness is given as an angle, 0..90 degrees. Divide
    26.                 // by 90 to get an alpha blending value in the range 0..1.
    27.                 float frac = angle / 90.0f;
    28.                 map[x, y, 0] = frac;
    29.                 map[x, y, 1] = 1 - frac;
    30.             }
    31.         }
    32.        
    33.         t.terrainData.SetAlphamaps(0, 0, map);
    34.     }
    35. }

    Any help would be appreciated, thanks.
     
  2. npsf3000

    npsf3000

    Joined:
    Sep 19, 2010
    Posts:
    3,830
    Take a look at line 28:

    Code (csharp):
    1. map[x, y, 0]= frac;
    x and y are currently defined as floats, yet the array indexor requires an int. Converting between float and int isn't trivial and can lead to loss of accuracy and precision so C# does not implicitly do it for you.

    After a quick look at your code I think the best solution would be to make x and y int's not floats.
     
  3. IamGawd

    IamGawd

    Joined:
    Jun 7, 2014
    Posts:
    21
    Fixed it, thank you! :D