Search Unity

Object not instantiating at the correct location

Discussion in 'Scripting' 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,153
    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,153
    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.
     
    drmimic017 and BeefCutlets like this.
  5. kor6k

    kor6k

    Joined:
    Jan 8, 2017
    Posts:
    50
    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
     
  8. glatteinfo

    glatteinfo

    Joined:
    Nov 17, 2019
    Posts:
    19
    thank you!! had the same problem <3
     
  9. drmimic017

    drmimic017

    Joined:
    Aug 4, 2021
    Posts:
    1
    answers in 2014 still solving questions in 2022
    ty
     
  10. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,455
    And unfortunately still necroing posts because of it. Please, hit the like button if you wish to show your appreication, rather than simply necroing threads.

    This isn't even a 2D specific post. I'll move it to the scripting forum.

    Thanks.