Advertisement

Problems with generating the map

Started by May 24, 2022 06:47 PM
1 comment, last by JoeJ 2 years, 6 months ago

https://www.youtube.com/watch?v=vfl2HyEar68&list=PLu2uAkIZ4shpPdCTIjEpvhD8U-RRM3Y2F&index=3 regarding to this video how can i add rotation to these random placed objects???

Here is all the code:

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

public class GenerateGrid : MonoBehaviour
{
   public GameObject blockGameObject;
   public GameObject ObjectToSpawn;


   private int worldSizeX = 10;

   private int worldSizeZ = 10;

   private int gridOffset = 5;

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


   // Start is called before the first frame update
   void Start()
   {
       for (int x = 0; x < worldSizeX; x++)
       {
           for (int z = 0; z < worldSizeZ; z++)
           {

               Vector3 pos = new Vector3(x * gridOffset, 0, z * gridOffset);
               GameObject block = Instantiate(blockGameObject, pos, Quaternion.identity) as GameObject;

               blockPositions.Add(block.transform.position);

               block.transform.SetParent(this.transform);

           }
       }
       SpawnObject();

   }


   private void SpawnObject()
   {
       for (int c = 0; c < 20; c++)
       {
           GameObject toPlaceObject = Instantiate(ObjectToSpawn, ObjectSpawnLocation(), Quaternion.identity);
       }
   }

   private Vector3 ObjectSpawnLocation()
   {
       int rndIndex = Random.Range(0, blockPositions.Count);
       Vector3 newPos = new Vector3(
           blockPositions[rndIndex].x,
           blockPositions[rndIndex].y +2.5f,
           blockPositions[rndIndex].z
           );

       blockPositions.RemoveAt(rndIndex);
       return newPos;

   }

}

NeoDym11 said:
GameObject toPlaceObject = Instantiate(ObjectToSpawn, ObjectSpawnLocation(), Quaternion.identity);

Idk Unity, but surely you want to provide a more interesting orientation than Quaternion.identity.
So probably your ObjectSpawnLocation() function should return such orientation as well.

If it's tree models, you probably want their up vector to remain (0,1,0).
So you could construct the quaternion like so: Quaternion orientation = Quaternion::FromAxisAndAngle (vec3(0,1,0), randomAngle);
Unity surely has such function in its Quaternion stuff or somewhere else.

This topic is closed to new replies.

Advertisement