Who to limit Sprite with tilt control, to screen boundaries?

66 views Asked by At

Im trying to limit a Sprite to screen edges. I'm using this script from unity manual and i've added a clamp to its movement. The issue is that the screen bounded is different with each resolution, and i cant figure out how to use "camera.WorldToViewportPoint" in here... My bound are currently 0.1F and 0.9F which i want to replace with real screen bounds or size. ATM this is working but the Sprite is moving in small bounds and not real screen size. Please help :)

here is my code so far:

public float speed = 10.0F;
public float posX;
public float posY;

void Update() {
    Vector3 dir = Vector3.zero;
    dir.x = Input.acceleration.x;
    dir.y = Input.acceleration.y;
    if (dir.sqrMagnitude > 1)
        dir.Normalize ();

    dir *= Time.deltaTime;
    transform.Translate (dir * speed);

    transform.position = new Vector3(Mathf.Clamp(transform.position.x, 0.1F, 0.9F),Mathf.Clamp(transform.position.y, 0.1F, 0.9F), 0);
}
0

There are 0 answers