1. Before updating to Windows 10 you may need to return your Pro license. More information here

Instantiate an object the same position as the Touch.Begain???

Discussion in 'iOS Development' started by Sam_Unity, Jan 31, 2012.

  1. Sam_Unity

    Sam_Unity

    Joined:
    Apr 25, 2011
    Messages:
    16
    Hi,

    I developing on IOS unity pro, and I have problem with instantiating an gameObject at the same postion when I have my fingure at the screen, I tried everything it does not really work the sample code is:


    if(Input.touchCount ==1){

    touch_n1 = Input.GetTouch(0);

    if (touch_n1.phase == TouchPhase.Began)
    {

    I nstantiate(carPrefap, touch.position , Quaternion.identity);


    }

    The result of that the GameObject is instantiated somewere else which is far a way from the touch position? Why? any help please???
     
  2. nikolic

    nikolic

    Joined:
    Oct 15, 2011
    Messages:
    162
  3. Sam_Unity

    Sam_Unity

    Joined:
    Apr 25, 2011
    Messages:
    16
    Yah but to use : Vector3 p = camera.ScreenToWorldPoint(new Vector3(100, 100, camera.nearClipPlane));

    I need to have vector3 in the param of "ScreenToWorldPoint" function while the touch.positon is vector2, how to solve this ???
     
  4. nikolic

    nikolic

    Joined:
    Oct 15, 2011
    Messages:
    162
  5. Sam_Unity

    Sam_Unity

    Joined:
    Apr 25, 2011
    Messages:
    16
    I have already done that, but still the object is not instantiated at the same position as the touch position, it drives me crazy sorry to say that ???
     
  6. Sam_Unity

    Sam_Unity

    Joined:
    Apr 25, 2011
    Messages:
    16
    if (touch_n1.phase == TouchPhase.Began)
    {
    //Debug.Log("Begain excuted !!!");

    //if(createItem){

    Vector3 p = camera.ScreenToWorldPoint(touch_n1.position);


    Instantiate(carPrefap, p , Quaternion.identity);
    }

    Shouldn't that do the work???
     
  7. Johnny Photon

    Johnny Photon

    Joined:
    Sep 4, 2011
    Messages:
    79
    Are you taking the position of your camera into account?

    var p : Vector3 = Camera.main.ScreenToWorldPoint (new Vector3 (pointerPosition.x,pointerPosition.y,-(Camera.main.transform.position.z + Gun.transform.position.z)));
    var delta : Vector3 = p - Gun.transform.position;

    Here I'm calculating a vector so I can determine the angle relative to my cannon, but I would think you would have to use the camera Z.

    Hope this helps.
     
  8. login4donald

    login4donald

    Joined:
    Jan 3, 2012
    Messages:
    463
    Johnny Photon's example should work, the camera should actually be a variable.
    Code (csharp):
    1.  
    2. var cam : Camera; // the camera in inspector if not the main camera
    3.  
    4. function Start () { //or do this if is main camera
    5. var cam = Camera.main.camera; //the .camera may not be needed
    6. }
    7. function Update () {
    8. if (touch_n1.phase == TouchPhase.Began) {
    9. if(createItem){
    10. }
    11. }
    12. }
    13.  
    Or if you're using the main camera then use johnny's example.
     
  9. Sam_Unity

    Sam_Unity

    Joined:
    Apr 25, 2011
    Messages:
    16
    HI login4donald I tried your solution but it did not work ... and johnny's example I could not figure out " Gun.transform.position", because all I have is Camera in the scene which the script is attached too, and the cube which I want to ray cast to from the camera point, all I need is to get the position and instantiating prefab but is like it never works no matter what I do or change !!! and I'm about to give up!!! So I hope there is concret solution or script that solves the problem.
     
    Last edited: Feb 1, 2012
  10. Johnny Photon

    Johnny Photon

    Joined:
    Sep 4, 2011
    Messages:
    79
    I should have just edited it. I thought you would just drop the gun position.
    I haven't tried this on a device but it works with the mouse in the editor and creates a cube prefab any time you click on the screen.

    Code (csharp):
    1. #pragma strict
    2.  
    3. private static var useMouse : boolean;
    4. static var pointerPosition : Vector2;
    5. var cubePreFab : GameObject;
    6.  
    7.  
    8. function Start () {
    9. }
    10.  
    11. function Update () {
    12.  
    13. var touch : Touch;
    14.  
    15.  
    16.    
    17. // get cursor position
    18.     if(useMouse) pointerPosition = Input.mousePosition;
    19.     else
    20.     {
    21.         if (Input.touchCount > 0)
    22.         {
    23.         touch = Input.touches[0];
    24.         if(touch.phase == TouchPhase.Began || touch.phase == TouchPhase.Moved)
    25.             pointerPosition = touch.position;
    26.         }
    27.     }
    28.    
    29. // see if we have an input event   
    30.     if ((Input.GetButtonDown("Fire1")  useMouse))  
    31.     {
    32.         CreateObject();
    33.     }
    34.     else if (Input.touchCount > 0  touch.phase == TouchPhase.Ended)
    35.     {
    36.         CreateObject();
    37.     }
    38. }
    39.  
    40. function CreateObject()
    41. {
    42.     var p : Vector3 = Camera.main.ScreenToWorldPoint (new Vector3 (pointerPosition.x,pointerPosition.y,-Camera.main.transform.position.z));
    43.     Instantiate(cubePreFab, p , Quaternion.identity);
    44. }
    You only need to use the camera position Z.
    Hope this helps.
     
  11. Sam_Unity

    Sam_Unity

    Joined:
    Apr 25, 2011
    Messages:
    16
    Hey guys I have managed it out with johnny's way, johnny you rock man, thank you very much.
     
  12. Johnny Photon

    Johnny Photon

    Joined:
    Sep 4, 2011
    Messages:
    79
    You are welcome. Glad I could help!