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

Instantiate Position / Camera problem

Discussion in 'Scripting' started by CodeNashor, Apr 29, 2015.

  1. CodeNashor

    CodeNashor

    Joined:
    Jan 6, 2014
    Posts:
    18
    Hell guys!

    I have a problem with my instantiate. I just want to create an object, which will be spawned EVERYTIME in the "right bottom corner" of the camera, REGARDLESS if i looking upwards, downwards or other things. (like a standard first person equip object).

    Code (csharp):
    1.  
    2. var instanceObject : GameObject = Instantiate(gameObject, mainCamera.GetComponent.<Camera>().transform.position + transform.forward * 0.1, mainCamera.GetComponent.<Camera>().transform.rotation));
    3.  
    4.   instanceObject.transform.parent = mainCamera.GetComponent.<Camera>().transform;
    5.   instanceObject.transform.localPosition.y -= 0.1;
    6.   instanceObject.transform.localPosition.x += 0.2;
    7.  
    This solution instantiate a object, which is nearly perfect. It will be spawned with the right position and so on. But if I look upwards or downwards, the rotation (and position) of the object is not correct and I cant figure out, why. (looks like the rotation is inverse, but I got no solution).

    Someone got a hint?
     
    Last edited: Apr 29, 2015
  2. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    8,999
  3. CodeNashor

    CodeNashor

    Joined:
    Jan 6, 2014
    Posts:
    18
    Ok i got it. LocalPosition of the prefab was not 0 as default, so it got positioned randomly.