Unity Community


Results 1 to 12 of 12

  1. Posts
    16

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

    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. Location
    Belgrade, Serbia
    Posts
    161
    touch.position is Vector2 instance. It's in Screen coordinates. You should convert it to value in World coordinates.
    Check out http://unity3d.com/support/documenta...orldPoint.html .


  3. 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. Location
    Belgrade, Serbia
    Posts
    161
    http://unity3d.com/support/documenta...r_Vector3.html

    Just pass the Vector2 instance, the z value will be set to zero.


  5. 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. 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. Posts
    74
    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. Location
    Louisville, Kentucky, United States
    Posts
    461
    Johnny Photon's example should work, the camera should actually be a variable.
    Code:  
    1. var cam : Camera; // the camera in inspector if not the main camera
    2.  
    3. function Start () { //or do this if is main camera
    4. var cam = Camera.main.camera; //the .camera may not be needed
    5. }
    6. function Update () {
    7. if (touch_n1.phase == TouchPhase.Began) {
    8. if(createItem){
    9. }
    10. }
    11. }
    Or if you're using the main camera then use johnny's example.


  9. 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 by Sam_Unity; 02-01-2012 at 07:54 AM.


  10. Posts
    74

    Sorry I wasn't clear

    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:  
    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. Posts
    16
    Hey guys I have managed it out with johnny's way, johnny you rock man, thank you very much.


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •