Search Unity

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

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

  1. Sam_Unity

    Sam_Unity

    Joined:
    Apr 25, 2011
    Posts:
    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
    Posts:
    162
  3. Sam_Unity

    Sam_Unity

    Joined:
    Apr 25, 2011
    Posts:
    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
    Posts:
    162
  5. Sam_Unity

    Sam_Unity

    Joined:
    Apr 25, 2011
    Posts:
    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
    Posts:
    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
    Posts:
    84
    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
    Posts:
    462
    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. Vector3 p = cam.ScreenToWorldPoint(touch_n1.position);
    11. Instantiate(carPrefap, p , Quaternion.identity);
    12. }
    13. }
    14. }
    15.  
    Or if you're using the main camera then use johnny's example.
     
  9. Sam_Unity

    Sam_Unity

    Joined:
    Apr 25, 2011
    Posts:
    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
    Posts:
    84
    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. if(Application.isEditor || Application.isWebPlayer) useMouse = true;
    10. }
    11.  
    12. function Update () {
    13.  
    14. var touch : Touch;
    15.  
    16.  
    17.    
    18. // get cursor position
    19.     if(useMouse) pointerPosition = Input.mousePosition;
    20.     else
    21.     {
    22.         if (Input.touchCount > 0)
    23.         {
    24.         touch = Input.touches[0];
    25.         if(touch.phase == TouchPhase.Began || touch.phase == TouchPhase.Moved)
    26.             pointerPosition = touch.position;
    27.         }
    28.     }
    29.    
    30. // see if we have an input event   
    31.     if ((Input.GetButtonDown("Fire1")  useMouse))  
    32.     {
    33.         CreateObject();
    34.     }
    35.     else if (Input.touchCount > 0  touch.phase == TouchPhase.Ended)
    36.     {
    37.         CreateObject();
    38.     }
    39. }
    40.  
    41. function CreateObject()
    42. {
    43.     var p : Vector3 = Camera.main.ScreenToWorldPoint (new Vector3 (pointerPosition.x,pointerPosition.y,-Camera.main.transform.position.z));
    44.     Instantiate(cubePreFab, p , Quaternion.identity);
    45. }
    You only need to use the camera position Z.
    Hope this helps.
     
  11. Sam_Unity

    Sam_Unity

    Joined:
    Apr 25, 2011
    Posts:
    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
    Posts:
    84
    You are welcome. Glad I could help!