Problem is that on my phone the enemy cars are spawning correctly from top to bottom but on my father's phones are spawning from the middle of screen to bottom.
As Joe mentions this could be due to the viewport discrepancies. Having devices with different aspect ratios the cars spawn point could change depending on the screen.
Here is the docs on how to use the viewport to calculate where in the world your objects will spawn: Camera.ViewportToWorldPoint
// This is the part that we will be replacing.
Vector2 enemigoRandomPos = new Vector2 (Random.Range(-maxPos, maxPos), transform.position.y);
Here is how I would go about it based on the code you provided:
// Set the offset of the X axis first. This should be fairly similar for most devices,
// if you find issues with it apply the same logic as the Y axis.
var x = Random.Range(-maxPos, maxPos);
// Here is where the magic happens, ViewportToWorldPoint converts a number between 0 and 1 to
// an in-world number based on what the camera sees. In this specific situation I am telling it: to use 0f, 1f
// which roughly translates to "At the top of the screen, on the left corner". Then storing the Y value of the call.
var y = Camera.main.ViewportToWorldPoint(new Vector2(0f, 1f)).y;
// Now that we have the x and y values, we can simply create the enemigoRandomPos based on them.
var enemigoRandomPos = new Vector2(x, y);
You can ofcourse delete all my comments and in-line the whole thing instead:
var enemigoRandomPos = new Vector2(Random.Range(-maxPos, maxPos), Camera.main.ViewportToWorldPoint(new Vector2(0f, 1f)).y);
A couple of things to keep in mind:
- Camera.main may not be defined, you'll need to find an instance of the camera (this is outside the scope of this question so I will let you google for that, if you have issues with it let me know and I'll be glad to provide further info)
- The X position can become weird at some aspect ratios, so I would suggest you consider also using the viewport calculation
- It would be more efficient to store these values (Y and the camera) on the Start method and only change them when the aspect ratio changes or the camera changes. This would help a bit on performance on older devices. More homework research. :)
- When troubleshooting this kind of issues, it can be beneficial to use static sprites (aka things that don't move) that show the problem. I would have spawned about 9 sprites in all corners + center of the screen to see where would the cars spawn from as a visual aid in the debugging process.
- Also providing screenshots when asking a question that is graphical in nature can help lots when people are trying to give you feedback, consider adding some the next time :D
And another problem is when you move your car to right or left it looks like cut diagonally.
Based on this description it sounds like some sort of clipping issue with the triangles of the car sprite and the background sprite. I suggest moving the background back or forth depending on where the camera is to avoid said clipping.