The problem here is that when I press/touch booth only the first thing i touched works. Example is if I touch the MOVE sprite first then the Gravity Changer sprite, both will not work only the Move script which I first touched.
First time on developing an android in unity3d
Movement Script:
void Update()
{
direction = 0;
int fingerCount = Input.touchCount;
for(int x = 0; x < fingerCount; x++)
{
Vector3 touchPos = Camera.main.ScreenToWorldPoint(Input.GetTouch(x).position);
RaycastHit2D hit = Physics2D.Raycast(touchPos, -Vector2.up);
if (fingerCount > 0)
{
if(hit.collider != null)
{
if(hit.collider.name.Equals("ToLeft"))
{
direction = -1;
}
else if(hit.collider.name.Equals("ToRight"))
{
direction = 1;
}
}
}
}
}
GravityChanger Script:
void Update()
{
direction = 0;
int fingerCount = Input.touchCount;
for(int x = 0; x < fingerCount; x++)
{
Vector2 touchPos = Camera.main.ScreenToWorldPoint(Input.GetTouch(x).position);
RaycastHit2D hit = Physics2D.Raycast(touchPos, -Vector2.up);
if (fingerCount > 0)
{
if(hit.collider.gameObject.name.Equals("GravityAbility") && Input.GetTouch(x).phase == TouchPhase.Began)
{
hasGravity = !hasGravity;
if(hasGravity)
{
hit.collider.transform.FindChild("GravityOff").gameObject.SetActive(false);
hit.collider.transform.FindChild("GravityOn").gameObject.SetActive(true);
}
else
{
hit.collider.transform.FindChild("GravityOff").gameObject.SetActive(true);
hit.collider.transform.FindChild("GravityOn").gameObject.SetActive(false);
}
}
if(hit.collider.gameObject.name.Equals("Restart"))
{
Application.LoadLevel(Application.loadedLevel);
}
}
}
}
↧