Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Kirbsssys · Oct 23, 2021 at 06:38 AM · instantiateprocedural generationperlin noise

I wanna know how to randomize perlin noise

I'm making a map out of blocks using perlin noise, the only problem is that the perlin noise doesn't change, so the map always stays the same, and I'd like to know how to fix that, heres the code

using System.Collections; using System.Collections.Generic; using UnityEngine;

public class GenerateInfiniteWorld : MonoBehaviour { public GameObject player; public GameObject blockGameObject;

 public int worldSizeX;
 public int worldSizeZ;

 public float noiseHeight;

 public float gridOffset;
 private Vector3 startPosition;

 private Hashtable blockContainer = new Hashtable();

 private List<Vector3> blockPositions = new List<Vector3>();

 void Start()
 {
     for (int x = -worldSizeX - 25; x < worldSizeX + 25; x++)
     {
         for(int z = -worldSizeZ - 25; z < worldSizeZ + 25; z++)
         {
             Vector3 pos = new Vector3(x * 1 + startPosition.x, generateNoise(x + xPlayerLocation, z + zPlayerLocation, 8f) * noiseHeight, z * 1 + startPosition.z);
             GameObject block = Instantiate(blockGameObject, pos, Quaternion.identity) as GameObject;
             blockContainer.Add(pos, block);
             blockPositions.Add(block.transform.position);
             block.transform.SetParent(this.transform);
         }
     }
 }

 private void Update()
 {
     if(Mathf.Abs(xPlayerMove) >= 1 || Mathf.Abs(zPlayerMove) >= 1)
     {
         for (int x = -worldSizeX; x < worldSizeX; x++)
         {
             for (int z = -worldSizeZ; z < worldSizeZ; z++)
             {
                 Vector3 pos = new Vector3(x * 1 + xPlayerLocation, generateNoise(x + xPlayerLocation, z + zPlayerLocation, 8f) * noiseHeight, z * 1 + zPlayerLocation);
                 if (!blockContainer.ContainsKey(pos))
                 {
                     GameObject block = Instantiate(blockGameObject, pos, Quaternion.identity) as GameObject;
                     blockContainer.Add(pos, block);
                     blockPositions.Add(block.transform.position);
                     block.transform.SetParent(this.transform);
                 }
             }
         }
     }
 }

 public int xPlayerMove
 {
     get
     {
         return (int)(player.transform.position.x - startPosition.x);
     }
 }

 public int zPlayerMove
 {
     get
     {
         return (int)(player.transform.position.z - startPosition.z);
     }
 }
 public int xPlayerLocation
 {
     get
     {
         return (int)Mathf.Floor(player.transform.position.x);
     }
 }

 public int zPlayerLocation
 {
     get
     {
         return (int)Mathf.Floor(player.transform.position.z);
     }
 }

 private Vector3 ObjectSpawnLocation()
 {
     int rndIndex = Random.Range(0, blockPositions.Count);

     Vector3 newPos = new Vector3(
         blockPositions[rndIndex].x,
         blockPositions[rndIndex].y + 0.5f,
         blockPositions[rndIndex].z
         );
     blockPositions.RemoveAt(rndIndex);
     return newPos;
 }
 private Vector3 ObjectSpawnLocation2()
 {
     int rndIndex = blockPositions.Count / 2;

     Vector3 newPos = new Vector3(
         blockPositions[rndIndex].x,
         blockPositions[rndIndex].y,
         blockPositions[worldSizeX / 2].z
         );
     blockPositions.RemoveAt(rndIndex);
     return newPos;
 }
 private Vector3 ObjectSpawnLocation3()
 {
     int rndIndex = Random.Range(0, blockPositions.Count);

     Vector3 newPos = new Vector3(
         blockPositions[rndIndex].x,
         blockPositions[rndIndex].y + 0.5f,
         blockPositions[rndIndex].z
         ) ;
     blockPositions.RemoveAt(rndIndex);
     return newPos;
 }

 private float generateNoise(int x, int z, float detailScale)
  {
     float xNoise = (x + this.transform.position.x) / detailScale;
     float zNoise = (z + this.transform.position.y) / detailScale;

     return Mathf.PerlinNoise(xNoise, zNoise);
  }
 }
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Kennedy15767 · Oct 23, 2021 at 11:30 AM

Perlin noise is a procedural texture primitive, a type of gradient noise used by visual effects artists to increase the appearance of realism in computer graphics. The function has a pseudo-random appearance, yet all of its visual details are the same size.

greatpeople

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by Llama_w_2Ls · Oct 25, 2021 at 11:07 AM

Use offsets. Add a random value between -10000 and 10000 to your perlin noise x and y values before you generate a point. However, these random values need to be initialized once in a start function for example. Don't randomize them every time you sample a point.


The offsets can also be kept predictable by using a seed value for your random number generator. For example:

 public string Seed;
 
 public int OffsetX;
 public int OffsetY;
 
 void Start()
 {
      var rand = new System.Random(Seed.GetHashCode());
 
      OffsetX = rand.Next(-10000, 10000);
      OffsetY = rand.Next(-10000, 10000);
 }

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

157 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Generating 3x3 "Terrain" using PerlinNoise 0 Answers

Better perlin noise map generation. 0 Answers

How do I make biomes with perlin noise 1 Answer

Checking if object intersects? 1 Answer

Strange texture2d behaviour 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges