- Home /
Convert Touch input to Mouse input C#?
Hello! I have a part of a game code that should work when put on a wordpress site. Right now it is only responding to touch, but not mouse button click. How should I go about converting this code to work with mouse click?
 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 
 public class  NoCrash : MonoBehaviour
 {
 
 bool moveAllowed;
 Collider2D col;
 
     // Start is called before the first frame update
     void Start()
     {
         col = GetComponent<Collider2D>();
     }
 
     // Update is called once per frame
     void Update()
     {
         if (Input.touchCount>0)
         {
             Touch touch = Input.GetTouch(0);
             Vector2 touchPosition = Camera.main.ScreenToWorldPoint(touch.position);
 
             if (touch.phase == TouchPhase.Began)
             {
                 Collider2D touchedCollider = Physics2D.OverlapPoint(touchPosition);
                 if (col == touchedCollider) 
                 {
                     moveAllowed = true;
                 }
             }
 
             if (touch.phase == TouchPhase.Moved)
             {
                 if (moveAllowed)
                 {
                     transform.position = new Vector2(touchPosition.x, touchPosition.y);
                 }
             }
 
             if (touch.phase == TouchPhase.Ended)
             {
                 moveAllowed = false;
             }
         }
     }
 }
               Comment
              
 
               
               
               Best Answer 
              
 
              Answer by exploringunity · May 24, 2020 at 12:27 AM
Hey @yenotsuns,
You probably want to take a look at Input.GetMouseButton, Input.GetMouseButtonDown, Input.GetMouseButtonUp, and Input.mousePosition as replacements for the Touch methods/properties you are using. Check out the documentation for the Input class here for more information.
Hope this helps!
Your answer
 
 
              koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                