0
\$\begingroup\$

I tried

      float pointer_x = Input.GetAxis("Mouse X");
      float pointer_y = Input.GetAxis("Mouse Y");
      if (Input.touchCount > 0)
      {
          pointer_x = Input.touches[0].deltaPosition.x;
          pointer_y = Input.touches[0].deltaPosition.y;
      } 

`

for Mouse X and Y but didn't do the results i wanted. I basically have shapes that I want to move on the X and Y axis. So when I move up and Down only move on Y axis, and when I move left and right only move on X axis.

The Raycast2D mobile version of this I'm not sure either.

         Vector2 worldPoint = Camera.main.ScreenToWorldPoint(Input.mousePosition);
 
         RaycastHit2D hit = Physics2D.Raycast(worldPoint, Vector2.zero);
 
         if (Input.GetMouseButtonDown(0))
         {
 
             for (int i = 0; i < empty.Length; i++)
             {
                 empty[i] = true;
             }
 
 
             if (hit.collider != null)
             {
                 startPos = transform.position;
 
                 offset = gameObject.transform.position -
                          Camera.main.ScreenToWorldPoint(
                          new Vector3(Input.mousePosition.x,
                          Input.mousePosition.y, screenPoint.z));
             }
         }
 
         if (Input.GetMouseButton(0))
         {
 
             if (hit.collider != null)
             {
 
                 Vector3 curScreenPoint = new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z);
                 Vector3 curPosition = Camera.main.ScreenToWorldPoint(curScreenPoint) + offset;
                 
 
                 if (!posY)
                 {
                     if (Input.GetAxis("Mouse X") > 0.1 ||
                         Input.GetAxis("Mouse X") < -0.1)
                     {
                         posX = true;
 
                         if (Mathf.Round(transform.position.y * 2f) * 0.5f == Mathf.Round(hit.transform.position.y * 2f) * 0.5f)
                         {
                             transform.position = new Vector3(curPosition.x, startPos.y, curPosition.z);
 
                         }
                     }
                 }
 
                 if (!posX)
                 {
                     if (Input.GetAxis("Mouse Y") > 0.1 ||
                         Input.GetAxis("Mouse Y") < -0.1)
                     {
                         posY = true;
                       
                         if (Mathf.Round(transform.position.x * 2f) * 0.5f == Mathf.Round(hit.transform.position.x * 2f) * 0.5f)
                         {
                             transform.position = new Vector3(startPos.x, curPosition.y, curPosition.z);
                         }
                        
                     }
                 }
 
 Mathf.Round(transform.position.y * 2f) * 0.5f + " " + Mathf.Round(hit.transform.position.y * 2f) * 0.5f);
             }
 
 
         }
 
         if (Input.GetMouseButtonUp(0))
         {
             posX = false;
             posY = false;
 
             if (hit.collider != null)
             {
                 transform.position = new Vector3(outputX, outputY);
 Mathf.Sign(hit.transform.position.x) * (Mathf.Abs((int)hit.transform.position.x) + 0.5f));
 
 Mathf.Round(transform.position.y * 2f) * 0.5f + " " + Mathf.Round(hit.transform.position.y * 2f) * 0.5f);
             }
 
             if (Mathf.Sign(transform.position.x) * (Mathf.Abs((int)transform.position.x) + 0.5f) >= 2.5f ||
                 Mathf.Sign(transform.position.x) * (Mathf.Abs((int)transform.position.x) + 0.5f) <= -2.5f ||
                 Mathf.Sign(transform.position.y) * (Mathf.Abs((int)transform.position.y) + 0.5f) >= 1.5f ||
                 Mathf.Sign(transform.position.y) * (Mathf.Abs((int)transform.position.y) + 0.5f) <= -4.5f)
             {
                 Destroy(gameObject);
             }
 
             clicked = false;
         }
\$\endgroup\$
1
  • \$\begingroup\$ There's no such thing as a mobile version of Raycast. Touch positions and deltas are returned in screenspace pixel units, just like mouse positions and deltas, so it's unclear what specific problem you need to solve here. \$\endgroup\$
    – DMGregory
    Nov 14 '20 at 2:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.