Question by 
               Jacooboo · Jul 15, 2021 at 11:22 AM · 
                c#android2d gametouch controlstouchscreen  
              
 
              Unity 2d android game How to "freeze" position on Y axis
So i'm creating an android game in Unity version 2020.3.13f1. I have a script so the player moves upwards immediately on start and then the player should move around and dodge obstacles and so on.. But i have a problem, i only want the player to be able to move on the X axis. The Y axis should be "locked" so he can't move up and down by touching on the screen. I can't figure out how to code this correctly, it always ends up in a mess somehow..
Here is my move by touch code:
 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class MoveByTouch : MonoBehaviour {
 
     // Update is called once per frame
     void Update()
     {
 
         if (Input.touchCount > 0)
         {
             Touch touch = Input.GetTouch(0);
             Vector3 touchPosition = Camera.main.ScreenToWorldPoint(touch.position);
             touchPosition.z = 0f;
             transform.position = touchPosition;
         }
     }
 }
               Comment
              
 
               
               
               Best Answer 
              
 
              Answer by unity_BUJQdslYm7EuFQ · Jul 15, 2021 at 01:40 PM
Here is your solution:
 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class MoveByTouch : MonoBehaviour
 {
     // Update is called once per frame
     void Update()
     {
         if (Input.touchCount > 0)
         {
             Touch touch = Input.GetTouch(0);
             Vector3 touchPosition = Camera.main.ScreenToWorldPoint(touch.position);
             transform.position = new Vector3(touchPosition.x, transform.position.y, 0);
         }
     }
 }
Your answer
 
 
              koobas.hobune.stream
koobas.hobune.stream 
                       
               
 
			 
                