Search Unity

  1. Looking for a job or to hire someone for a project? Check out the re-opened job forums.
    Dismiss Notice
  2. Unity 2020 LTS & Unity 2021.1 have been released.
    Dismiss Notice
  3. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

Object not instantiating at the correct location

Discussion in '2D' started by BeefCutlets, Jan 26, 2017.

  1. BeefCutlets

    BeefCutlets

    Joined:
    Dec 8, 2014
    Posts:
    3
    Hello,

    I have searched everywhere on this site looking for similar problems but couldn't find any. After a couple days of trying to tinker with it I am coming to you guys for help because I can't figure it out for the life of me.

    The problem is that the bullet object for my character is instantiating at a random, set location away from the desired spot. See this picture -> http://imgur.com/a/B76KP

    My original thought was that it was spawning at world coordinates instead of local, but I couldn't get that to work. Next I figured my prefab have a wonky alignment, but after setting all of my prefab coordinates it still doesn't work. Even hard coding in the coords still gave me the same result. This is also happening when I instantiate the enemy's bullets, but I figure if I can get one fixed I can fix the other too

    Here is the relevant code for my player controller...


    And here is code for my bullet object

    Note the comment which still always pops up with the proper coords despite spawning in a different location.


    If anyone had some ideas I would be very appreciative

    Thanks!
     
  2. vakabaka

    vakabaka

    Joined:
    Jul 21, 2014
    Posts:
    1,154
    is the firePos child from moving player ?

    and if you are using "public Transform firePos;" then you have allready transform reference, so instead "firePos.transform.position" you can just use "firePos.position". Use "firePos.transform.position" for GameObject like
    "public GameObject firePos;"
     
  3. BeefCutlets

    BeefCutlets

    Joined:
    Dec 8, 2014
    Posts:
    3
    Yes, firePos is the child of the player. I also switched to just firePos.position but I still get the same result
     
  4. vakabaka

    vakabaka

    Joined:
    Jul 21, 2014
    Posts:
    1,154
    I am sure, the solution is easy, but I cannot see it o_O

    Is the bullet builded from many object and the renderer is little offset ? Try place the bullet prefab in the scene and change its position to zero, then check if it is at zero point (place something other at zero, the camera as example, then it should be in middle of the camera). Or something is wrong with z-position and rotation.
     
    BeefCutlets likes this.
  5. kor6k

    kor6k

    Joined:
    Jan 8, 2017
    Posts:
    38
    where do you define the position of firepos? I don't see it in your code...
     
  6. BeefCutlets

    BeefCutlets

    Joined:
    Dec 8, 2014
    Posts:
    3
    That was it! I knew that the objects that made up the "bullet" objects had an offset, but I thought it was just so that they were spaced properly. Apparently not though! Thank you
     
  7. Sanjay112000

    Sanjay112000

    Joined:
    Nov 9, 2020
    Posts:
    10
    Thanks vakabaka
     
unityunity