samedi 25 mai 2019

gameObject out of screen edge

I have this piece of code from my 2D game character. It's working fine on respawning my character on random Screen positions, but the problem is that this code works with my game caharacter Center, I mean it generates the center position to a Random Range and there on the edge of the screen my chracter is going half out of the screen... I need this code working on any Android device and i dont know how i can solve this. Anyone a simple solution?

Sorry for my beginner english.

//Generate Random X
    float generateX()
    {
        float x = Random.Range(Camera.main.ScreenToWorldPoint(new Vector2(0, 0)).x, Camera.main.ScreenToWorldPoint(new Vector2(Screen.width, 0)).x);
        return x;
    }

    //Generate Random Y
    float generateY()
    {
        float y = Random.Range(2.0f, Camera.main.ScreenToWorldPoint(new Vector2(0, Screen.height)).y);
        return y;
    }

void generateCoordinates()
    {
        transform.position = new Vector2(generateX(), generateY());
    }




Aucun commentaire:

Enregistrer un commentaire