-2

I want to programm a terrain generator in Unity and already have some working code for a Perlin Noise Terrain Generator. Im currently using Unity (using c#) and i cant figure out how to seperate the generation of the terrain depth(z)(the height of the mountains/depth of the valleys which i want to calculate using a Sqrt function) depending on x and the terrain depth depending on y. My problem is, when i try to convert the floats into ints, i would have to round them up or else they wont work, but i want to calculate the Terraindepth smoothly, is there a way i can combine those 2 heights into 1 without roughening the terrain?

float CalculateHeights(int x, int y)
{
    float xCoord = x;

        private int xz = float xCoord; 

    float yCoord = (float)y / height; 

        private int yz =float yCoord;

    int z = (xz + yz) / 2;

    return Mathf.Sqrt(z);
}
  • There's a lot going on with this code. First, you can't specify a "private" modifier within a C# function. Secondly, what is "height" and where is it declared? You don't need to specify the variable type when you reference the variable a second time. If you were trying to cast it to a float, you would need to wrap it in parenthesis like you're doing with "(float)y". However, you don't need to cast to a float when it's already a float. Finally, when you assign the result to an int, you would need to either cast or convert it. This code won't compile. You'll need to fix its issues first. – computerGuyCJ Jul 05 '22 at 21:57
  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. – Community Jul 05 '22 at 22:00

1 Answers1

0

If you're trying to use Perlin Noise in Unity, you can always use the inbuilt generator.

Ex:

float height = Mathf.PerlinNoise(X,Y);

Warning though: If you don't use floating point values for X and Y with this function, you'll have issues with always getting the same value.

Hope this helps.

Docs - https://docs.unity3d.com/ScriptReference/Mathf.PerlinNoise.html

  • Thanks, but im trying to get away from using perlin noise, i want to experiment with other mathematical functions as well, like square ore other stuff, so i want to programm a generator that calculates the x and y direction seperately, so0 im asking for a calculator that makes int x and int y to one int z like that: z = (x + y) / 2 and then i want to use z for a mathematical function. – Paul Riehm Jul 06 '22 at 08:28
  • Sorry, I was trying to get a terrain but therefore i need 2 functions, not one. – Paul Riehm Jul 06 '22 at 08:31