Hi Developers,
I have a simple tilt code and a simple flip code and I want to work these codes together. My game is simple 2D top down game and I want my character do flip when the phone is tilting. Character have to look right when tilting right and have to look left when tilting left.
public class Tilt : MonoBehaviour {
public float speed = 50.0F;
bool facingRight = true;
void Update() {
Vector3 dir = Vector3.zero;
dir.x = Input.acceleration.x;
if (dir.sqrMagnitude > 1)
dir.Normalize();
if (dir.sqrMagnitude > 1 && !facingRight)
Flip ();
else if (dir.sqrMagnitude < 1 && facingRight)
Flip ();
dir *= Time.deltaTime;
transform.Translate(dir * speed);
}
void Flip()
{
facingRight = !facingRight;
Vector3 theScale = transform.localScale;
theScale.x *= -1;
transform.localScale = theScale;
}
}