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
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

How to convert unit to pixel

Discussion in 'Scripting' started by gstahsn, Jan 30, 2015.

  1. gstahsn

    gstahsn

    Joined:
    Dec 26, 2014
    Posts:
    11
    Hi guys!
    if(Input.touchCount==1)
    {

    Touch parmak=Input.GetTouch(0);
    float x=parmak.position.x/Screen.width;
    float y=parmak.position.y/Screen.height;
    Instantiate(obj,new Vector3(x,y,0),Quaternion.identity);

    }

    My phone does not occur in the object(Android).
    Where is the error. Please help me;
     
  2. DylanYasen

    DylanYasen

    Joined:
    Oct 9, 2013
    Posts:
    50
    this is about converting position between "world space" and "screen space".

    instantiate needs world space.

    and I'm pretty sure touch.position will return screen position.


    you need to convert between these using camera.

    http://docs.unity3d.com/ScriptReference/Camera.ScreenToWorldPoint.html

    vector3 pos = Camera.main.ScreenToWorldPoint(touch.pos);

    instantiate(...,pos,...);


    hope it helps
     
  3. gstahsn

    gstahsn

    Joined:
    Dec 26, 2014
    Posts:
    11
    @DylanYasen
    if(Input.touchCount==1)
    {

    Touch parmak=Input.GetTouch(0);
    Vector3 pos=Camera.main.ScreenToWorldPoint(new Vector3(parmak.position.x,parmak.position.y,0));
    Instantiate(obj,pos,Quaternion.identity);


    } not work . please help me