R
R
RavQs2021-07-12 20:10:16
Unity
RavQs, 2021-07-12 20:10:16

How to spawn an object in two random positions?

how to create a platform so that it is created exactly in two positions, and not between them?

[SerializeField] private GameObject _prefab;

[ContextMenu("Spawn")]
public void OnSpawn()
{
Vector3 platformPosition = new Vector3(Random.Range(-10f,10f), transform.position.y);
var _instantiate = Instantiate(_prefab, platformPosition, Quaternion.identity);
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
AnanasikDev, 2021-07-14
@AnanasikDev

As an option, choose a random element from an array of positions.

var positions = new Vector3[2] { new Vector3(10, 0, 0), new Vector3(-10, 0, 0)};
Vector3 platformPosition = positions[Random.Range(0, positions.Length)];
var _instantiate = Instantiate(_prefab, platformPosition, Quaternion.identity);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question