- Home /
Who to limit Sprite with tilt control, to screen boundaries?
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;
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);
}
Your answer
Follow this Question
Related Questions
How to set screen boundaries? 1 Answer
Get screen bounds with **rotated** ortho camera?? 2 Answers
clamp input.acclereation within screen width 2 Answers
Getting Screen Bounds 1 Answer