Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

I need help with Iphone touch screen

Discussion in 'Scripting' started by TheOrangeNinja2245, Jan 5, 2020.

  1. TheOrangeNinja2245

    TheOrangeNinja2245

    Joined:
    Nov 24, 2018
    Posts:
    12
    I have a script that is meant to move a cube to where ever you touch on the screen, but it keeps outputting the camera position with z at 0. im using unity v2019.1.12f1 with UnityRemote5 on iphone 6s (ios v13.3). Please help!
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class TouchObjectTest : MonoBehaviour
    6. {
    7.  
    8.     // Start is called before the first frame update
    9.     void Start()
    10.     {
    11.      
    12.     }
    13.  
    14.  
    15.     void Update()
    16.     {
    17.         if(Input.touchCount > 0)
    18.         {
    19.             Touch touch = Input.GetTouch(0);
    20.             Vector3 touchPosition = Camera.main.ScreenToWorldPoint(touch.position);
    21.             touchPosition.z = 0f;
    22.             transform.position = touchPosition;
    23.             Debug.Log("touched" + touchPosition);
    24.         }
    25.     }
    26.  
    27.     private void OnMouseDown()
    28.     {
    29.      
    30.     }
    31. }
     
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,151
    Umm...you're setting touchPosition.z = 0f;

    What did you expect it to debug?
     
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,971
    The z that you give to the camera transformation function is how far you want the 3D point to be into the scene (from the camera).

    Do NOT modify the value you get back unless that is what you want, which I doubt it is.

    See the docs here: https://docs.unity3d.com/ScriptReference/Camera.ScreenToWorldPoint.html

    I quote from that doc: "The z position is in world units from the camera."
     
  4. TheOrangeNinja2245

    TheOrangeNinja2245

    Joined:
    Nov 24, 2018
    Posts:
    12
    And when i remove the line of code the cube goes to where the camera is positioned not where i touched on screen
     
  5. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,971
    When you touch the screen you are NOT touching a point in the world. You are actually touching a line in the world, an infinite line starting at the camera and going forward under your finger. That's the reason for supplying that .z value going in.

    Correct. That is 0 meters into the scene from the camera. When you take
    touch.position
    directly, that means (x,y,0) and that 0 means 0 meters into the camera.

    What happens when you REMOVE line 21 above and instead supply the .Z coordinate going INTO the function, not coming out of?
     
  6. TheOrangeNinja2245

    TheOrangeNinja2245

    Joined:
    Nov 24, 2018
    Posts:
    12
    Nevermind i found my answer.
     
    Last edited: Jan 5, 2020