- Home /
Question by
dipsvirag4555 · Aug 05, 2019 at 06:53 AM ·
positioncube
Cube Movement using Touch??
cube is work fine left,right,up,down fine
problem is
cube up and down : but when i m comment a code left and right then work up and down cube left and right : but when i m comment a code up and down then work left and right
Rigidbody2D rb;
public float movespeed;
// Use this for initialization
void Start()
{
rb = GetComponent<Rigidbody2D>();
movespeed = 10f;
Debug.Log("Screen Width : " + Screen.width); //1080
Debug.Log("Screen Height : " + Screen.height); //1920
}
// Update is called once per frame
void Update()
{
if (Input.touchCount > 0)
{
Touch touch = Input.GetTouch(0); ///hold the information about this touch
switch (touch.phase)
{
case TouchPhase.Began:
// problem is left and right work but when i comment a code up and down
if (touch.position.x < Screen.width / 2) //left
rb.velocity = new Vector2(-movespeed, 0f);
DebugPanel.Log("x", "first W:", touch.position.x < Screen.width / 2);
if (touch.position.x > Screen.width / 2) //right
rb.velocity = new Vector2(movespeed, 0f);
DebugPanel.Log("x", "Second W:", touch.position.x > Screen.width / 2);
// problem is up and down work but when i comment a code left and right
if (touch.position.y < Screen.height / 2) //Up
rb.velocity = new Vector2(0f, -movespeed);
DebugPanel.Log("y", "First H :", touch.position.y < Screen.height / 2);
if (touch.position.y > Screen.height / 2) //Down
rb.velocity = new Vector2(0f, movespeed);
DebugPanel.Log("y", "Second H:", touch.position.y > Screen.height / 2);
break;
case TouchPhase.Ended:
rb.velocity = new Vector2(0f, 0f); //when cube is ended then velocty is0
break;
}
}
}
how to move my cube when i touch left then going left, when i touch right then cube going right, when i touch upthen cube going up , when i touch down then cube going down.. hOW??
cube is going out of screen in mobile??
Comment