- Home /
 
Moving player to mouse click
I was planning on publishing my game to the google play store but I've run into a problem. Now I need to completely redo my movement script. Right now it is set up for mobile touch input and I'd like to change it to work with a mouse. Essentially I want to set it up in a way where the player teleports to the mouse position but only on the x axis as that is how the game works. How can I do this? Thanks.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Movement : MonoBehaviour {
 public GameObject gameOverMenu;
 public GameObject highScoreNumber;
 void Update()
 {
     if (Input.touchCount > 0)
     {
         Touch touch = Input.GetTouch(0);
         Vector3 touchPosition = Camera.main.ScreenToWorldPoint(touch.position);
         touchPosition.z = 0f;
         touchPosition.y = 0f;
         transform.position = touchPosition;
     }
 }
 void OnCollisionEnter2D(Collision2D collision)
 {
     if (collision.gameObject.name == "Obstacle(Clone)")
     {
         StartCoroutine("GameOverMenu");
         Time.timeScale = 0;
     }
 }
 IEnumerator GameOverMenu()
 {
     yield return new WaitForSecondsRealtime(.8f);
     gameOverMenu.SetActive(true);
     highScoreNumber.SetActive(true);
 }
 
               }
Answer by shahprateek317 · Nov 03, 2020 at 05:22 AM
You should simply be able to use:
Input.GetMouseButton(0) instead of Input.GetTouch(0)
Input.mousePosition instead of touch.position
Answer by jamesorion44 · Nov 03, 2020 at 12:29 PM
@shahprateek317 What would I replace Touch with because I get an error saying "cannot implicitly convert bool to UnityEngine.Touch."
if (Input.GetMouseButton(0)) {
         Touch touch = Input.GetMouseButton(0);
         Vector3 mousePosition = Camera.main.ScreenToWorldPoint(touch.position);
         mousePosition.z = 0f;
         mousePosition.y = 0f;
         transform.position = mousePosition;
     }
 
              Your answer
 
             Follow this Question
Related Questions
My Touch Movement Script is Not Working Correctly 1 Answer
Object some times goes off screen 0 Answers
Why are my controls laggy 1 Answer
Help with rotating a sprite with mouse. 2 Answers