How can i create/spawn some objects and set random angle for each one ?
What i want is to make that when i Instantiate the objects each one will face another direction only on the y axis for now. So it will look like every object to face another direction. And i need to make that the random numbers will be as much as possible not too close to each other. For example if the first object is at angle 12 so the second angle must be less or high then 12 enough to make the object look facing another direction and not look like they are facing the same direction.
This is the script i'm using. In the bottom i have a method rndNumbers i'm calling this method once in the Start function. Then in the loop i added the line:
o.transform.Rotate(0.0f,randomNumbers[i],0.0f);
But it's far from what i want.
This is the script:
using System;
using UnityEngine;
using Random = UnityEngine.Random;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
public class CloneSpaceShips : MonoBehaviour {
public GameObject Spaceship;
[HideInInspector]
public GameObject[] spaceships;
// for tracking properties change
private Vector3 _extents;
private int _spaceshipCount;
private float _spaceshipSize;
private List<float> randomNumbers = new List<float> ();
/// <summary>
/// How far to place spheres randomly.
/// </summary>
public Vector3 Extents;
/// <summary>
/// How many spheres wanted.
/// </summary>
public int SpaceShipCount;
public float SpaceShipSize;
// Use this for initialization
void Start () {
rndNumbers ();
Clone();
spaceships = GameObject.FindGameObjectsWithTag("SpaceShip");
}
private void OnValidate()
{
// prevent wrong values to be entered
Extents = new Vector3(Mathf.Max(0.0f, Extents.x), Mathf.Max(0.0f, Extents.y), Mathf.Max(0.0f, Extents.z));
SpaceShipCount = Mathf.Max(0, SpaceShipCount);
SpaceShipSize = Mathf.Max(0.0f, SpaceShipSize);
}
private void Reset()
{
Extents = new Vector3(250.0f, 20.0f, 250.0f);
SpaceShipCount = 100;
SpaceShipSize = 20.0f;
}
// Update is called once per frame
void Update () {
}
private void Clone()
{
if (Extents == _extents && SpaceShipCount == _spaceshipCount && Mathf.Approximately(SpaceShipSize, _spaceshipSize))
return;
// cleanup
var ShipsToDestroy = GameObject.FindGameObjectsWithTag("SpaceShip");
foreach (var t in ShipsToDestroy)
{
if (Application.isEditor)
{
DestroyImmediate(t);
}
else
{
Destroy(t);
}
}
var withTag = GameObject.FindWithTag("Terrain");
if (withTag == null)
throw new InvalidOperationException("Terrain not found");
for (var i = 0; i < SpaceShipCount; i++)
{
var o = Instantiate(Spaceship);
o.tag = "SpaceShip";
o.transform.SetParent(base.gameObject.transform);
o.transform.localScale = new Vector3(SpaceShipSize, SpaceShipSize, SpaceShipSize);
// get random position
var x = Random.Range(-Extents.x, Extents.x);
var y = Extents.y; // sphere altitude relative to terrain below
var z = Random.Range(-Extents.z, Extents.z);
// now send a ray down terrain to adjust Y according terrain below
var height = 10000.0f; // should be higher than highest terrain altitude
var origin = new Vector3(x, height, z);
var ray = new Ray(origin, Vector3.down);
RaycastHit hit;
var maxDistance = 20000.0f;
var nameToLayer = LayerMask.NameToLayer("Terrain");
var layerMask = 1 << nameToLayer;
if (Physics.Raycast(ray, out hit, maxDistance, layerMask))
{
var distance = hit.distance;
y = height - distance + y; // adjust
}
else
{
Debug.LogWarning("Terrain not hit, using default height !");
}
o.transform.Rotate(0.0f,randomNumbers[i],0.0f);
// place !
o.transform.position = new Vector3(x, y, z);
}
_extents = Extents;
_spaceshipCount = SpaceShipCount;
_spaceshipSize = SpaceShipSize;
}
public void rndNumbers()
{
var currentValue = 90f;
var increment = 90f / SpaceShipCount;
for(int i=0;i<SpaceShipCount;i++) {
randomNumbers.Add(currentValue);
currentValue += increment;
}
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612095056im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
I create material with script but it does not render right 0 Answers
Creating Splines from empties in script 0 Answers
how to ask if something is not true for an amount of time 2 Answers
How can i check if the alpha color or the material color is transparent ? 1 Answer
How do I make somthing happen when the Player reaches a certain x, y, z position? 0 Answers