0

I'm starting to make games with Unity3D since one or two months ago. I've made my 1st game for Android and it is working perfectly on my phone (Samsung Galaxy S6) and on the emulator (Genymotion with differents virtual devices) but when I tried it on my father's phones (Nexus 5, Xperia Z1 & Z3) I realized it is working bad.

The game is a 2D car traffic racer so you have to dodge all the cars that the spawner is creating on random positions at the X axis. I don't know too much about Unity3d so I can't explain it better, sorry... :(

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 sceen to bottom. And another problem is when you move your car to right or left it looks like cut diagonally.

Here it is the code of the enemys spawner:

public class SpawnerEnemigos : MonoBehaviour {

public GameObject[] cochesEnemigos;
int cocheEnemigoID;
public float maxPos = 2f;
public float delayTimer = 0.5f;
private float timer;

// Use this for initialization
void Start () {
    timer = delayTimer;
}

// Update is called once per frame
void Update () {

    timer -= Time.deltaTime;
    if (timer <= 0) {
        Vector2 enemigoRandomPos = new Vector2 (Random.Range(-maxPos, maxPos), transform.position.y);
        cocheEnemigoID = Random.Range(0,7);
        Instantiate (cochesEnemigos[cocheEnemigoID], enemigoRandomPos, transform.rotation);
        timer = delayTimer;
    }
}

}

Fattie
  • 27,874
  • 70
  • 431
  • 719
AlexMnrs
  • 3
  • 2
  • 2
    Bear in mind that a game should not depend on a phones screen dimensions. You have probably hardcoded some values that are correct for the screen resolution of your S6 but not for other phones. With no code attached it is impossible for someone to help you more drastically. – pleft Feb 23 '16 at 15:14
  • 1
    Along with what @elefasGR mentioned above, which is most likely the issue, your assets may also not be sized correctly for other screen densities, causing their positioning to be off on various screen sizes. The first thing I would do is try to emulate your father's phones and match their exact screen sizes and densities, then start working with your code from there. – NoChinDeluxe Feb 23 '16 at 15:24
  • 1
    Show your code where you pick the random position to spawn at. – Buddy Feb 23 '16 at 15:28
  • Indeed. It's actually ***incredibly difficult engineering*** to make games work perfectly on all shape devices. It's one of those things they "just don't mention" when you pick up the easy-peasy Unity manual. – Fattie Feb 23 '16 at 15:32
  • Edited my question with the code of enemys spawner. I'm reading all your answers but still I don't know what to do :( thank you very much for the time spent answering me... – AlexMnrs Feb 23 '16 at 15:34
  • What they are saying is you need to instantiate your vehicles based off of the screens dimensions, as opposed to picking a random number to spawn them at. You could look at ViewportToWorldPoint – Savlon Feb 23 '16 at 15:41

1 Answers1

0

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.

Katsuke
  • 590
  • 4
  • 21