Hello!
I got a movable object. In the Object’s hierarchy it has empty game object called Spawner. I want it to place a small cube near to object randomly when space bar is pressed:
void Update()
{
if (Input.GetKeyDown(KeyCode.Space))
{
float distanceToCurrent = Vector3.Distance(targetObject.transform.position, transform.position);
if (distanceToCurrent <= 6)
{
//Instantiate cube randomly
int cubePointX = Random.Range(-10, 70);
int cubePointY = Random.Range(-10, 70);
Vector3 cubePos = new Vector3(cubePointX, 1, cubePointY);
Instantiate(cubePrefab, cubePos, Quaternion.identity);
}
}
}
I played with Random.Range
's coordinates to fulfill my need for awhile since I couldn’t resolved it.
Any idea would be gladly appreciated…