Instantiating gun does not appear where I want!

Hey all,
I currently have an issue where I start off with a simple pistol.
Upon pressing “2” an SMG should be instantiated and the previous gun destroyed.
However, while the pistol successfully starts off in the right position and stays there even when switching back to it the SMG is not so obedient. It spawns about 200m away in the x axis and I do not know how to fix it.
The code I use to spawn the guns:

using UnityEngine;
using System.Collections;

public class Player : MonoBehaviour
{
    GameObject startWeapon;
    Vector3 startPos;
    Quaternion startRot;
    GameObject equippedWeaponGO;

    void Start ()
    {
        startWeapon = GameObject.FindGameObjectWithTag("Equipped");
        startPos = startWeapon.transform.position;
        startRot = startWeapon.transform.rotation;
        equippedWeaponGO = startWeapon;
    }

    void Update ()
    {
        if(Input.GetKeyDown(KeyCode.Alpha1))
        {
            EquipWeapon(0);
        }
        else if(Input.GetKeyDown(KeyCode.Alpha2))
        {
            EquipWeapon(1);
        }
        else if(Input.GetKeyDown(KeyCode.Alpha3))
        {
            EquipWeapon(2);
        }
        else if(Input.GetKeyDown(KeyCode.Alpha4))
        {
            EquipWeapon(3);
        }
    }

    void EquipWeapon(int toEquip)
    {
        if(GameManager.Instance.playersGuns.Count > toEquip)
        {
            Destroy(equippedWeaponGO);
            GameObject newGun = Instantiate(GameManager.Instance.playersGuns[toEquip], startPos, startRot) as GameObject;
            newGun.transform.SetParent(Camera.main.transform);
            newGun.tag = "Equipped";
            equippedWeaponGO = newGun;
        }
        else
        {
            Debug.Log("No Weapon in this slot!");
        }
    }

    void OnTriggerEnter(Collider col)
    {
        Debug.Log(col.gameObject);
        if(col.gameObject.tag == "DeathBarrier")
        {
            Debug.Log("You have collided with " + col.gameObject);
            GameManager.Instance.Die();
        }
    }
}

Here are a couple of pictures, one where the gun is positioned fine and the other is an example of it randomly positioning itself somewhere else.

EDIT:
It works a single time when I compile it without setting the pos and rot when instantiating then back again with setting the pos and rot. Could this be a bug?

It’s probably not a bug. Have you checked the child objects of your SMG prefab to make sure their localPositions in their transforms are set to 0?

What Hyblademin said, take a look at the prefab of the SMG you are instantiating and make sure the positions are zeroed. It will only do what you tell it to do.

I can confirm that they are zeroed, the parent and the child mesh.
I would provide a screenshot but a little busy right now.

Bump!
Any ideas?
Still only works one time after compiling.

startPos and startRot are set in Start and never updated again so if the player moves I would expect those values to not be accurate relative to the player’s new position.

I agree, it looks like your startPos is just never updated and you use it to set it instead of setting it to gameObject.transform.position

I adjusted the code a little - changing it so that instead of getting the co-ordinates at the start, it gets them from the currently equipped gun right before instantiating.
However, I now have a new issue. If I spawn the gun that I currently have equipped whilst having it equipped it start to rotate on the spot.
I created a quick video to try and show it.

(It is still processing so may only be 480p for now)

I can’t really see the new code (the video is still 480p for me), but I have a suggestion. You don’t actually need to instantiate the guns at all. You can have all the guns in place, on top of each other, and set all guns except the selected one to inactive. You won’t have to deal with positioning at all.

2 Likes