random spawn enemy problem
I'm trying to make my game object spawn randomly from a certain point X to another point X, I'm following a tutorial that has an obsolete code.
var x1 = transform.position.x - renderer.bounds.size.x/5;
var x2 = transform.position.x + renderer.bounds.size.x/5;
var spawnPoint = new Vector2(Random.Range(x1, x2), transform.position.y);
the unity change to
var x1 = transform.position.x - GetComponent<Renderer>().bounds.size.x/5;
var x2 = transform.position.x + GetComponent<Renderer>().bounds.size.x/5;
but the object drops from the same position, so I found that changing to
var x1 = transform.position.x - GetComponent<Renderer>().bounds.size.x-5;
var x2 = transform.position.x + GetComponent<Renderer>().bounds.size.x+5;
it starts with spawn in a random area but it doesn't respect the limits
if I change to
var x1 = transform.position.x - GetComponent<Renderer>().bounds.size.x+5;
var x2 = transform.position.x + GetComponent<Renderer>().bounds.size.x-5;
it doesn’t reach X=2, I had to make it 9 for spawn to X=4.5, would anyone know how to help me? and say the reason so you can understand
Your answer
Follow this Question
Related Questions
How to disable raycast/groundchecker on OnTriggerEnter? 0 Answers
Accessing script from another object 1 Answer
Disable and enable emission property of a material via C# script? 0 Answers
I am trying to reference a bool from another script but the only value that it returns is false 0 Answers
Make an object stationary until player is colliding with it, then move along path. 1 Answer