Search Unity

  1. We are migrating the Unity Forums to Unity Discussions by the end of July. Read our announcement for more information and let us know if you have any questions.
    Dismiss Notice
  2. Dismiss Notice

Question Why when touching the phone screen to change the player position the player is gone away vanish?

Discussion in 'Input System' started by Chocolade, Jul 13, 2023.

  1. Chocolade

    Chocolade

    Joined:
    Jun 19, 2013
    Posts:
    947
    I want to make that where I touch with my finger on the phone screen the player will move to that position.

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using UnityEngine.UI;
    4.  
    5. public class MobileControls : MonoBehaviour
    6. {
    7.     private void Update()
    8.     {
    9.         if (Input.touchCount > 0)
    10.         {
    11.             Touch touch = Input.GetTouch(0);
    12.             Vector3 touchPosition = Camera.main.ScreenToWorldPoint(touch.position);
    13.             touchPosition.z = 0f;
    14.             transform.position = touchPosition;
    15.         }
    16.     }
    17. }
    18.  
    no matter if the camera is setting to perspective or to orthographic the player is just moving somewhere away far away from the camera.

    the script is attached to the player.

    on my phone android I'm using unity remote application and the Developer options enabled on my phone already.
     
  2. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    8,417
    Have you tested this with mouse inputs to see if your positioning is correct? It's likely not the inputs, but your calculations.
     
  3. ijmmai

    ijmmai

    Joined:
    Jun 9, 2023
    Posts:
    188
    Seems you are using Brackeys code.
    Put some Debug.Log() messages between the commands to see what values your code is working with.
     
  4. Neto_Kokku

    Neto_Kokku

    Joined:
    Feb 15, 2018
    Posts:
    1,751
    According to the documentation, the Z component of the screen space vector passed to ScreenToWorldPoint defines the depth of the returned world position relative to the camera.

    Since you're not setting the value of touchPosition.z, it's probably placing the object right at the camera position (zero depth).