Instantiating gun to FPSCamera, positioning problem
I've been trying to make a gun stick to the camera view of a FPSController for some time now. I don't know what I'm doing wrong though. I pick up and destroy the gun and thereafter I instantiate an instance of the Gun and child it to the camera on the FPSController. I have some offset that I want to apply but it seems as if the offset wont apply when I have the gun picked up and the gun sticks to the top of the view (seen below)
I've tried to alter the offset from the editor also at runtime, but it seems as if the gun is stuck or frozen in its position and rotation for some reason. I've tried a couple of things here but think I need some expert help on this one. The code for instantiating the gun is below:
public void GunPickup()
{
float xOffset = (transform.position.x + xOffsetGun);
float yOffset = (transform.position.y + yOffsetGun); // set from editor
float zOffset = (transform.position.z + zOffsetGun);
Vector3 startPos = new Vector3(xOffset, yOffset, zOffset);
gunObj = (GameObject) Instantiate(gunPrefab,startPos,transform.rotation);
gunObj.transform.SetParent(transform,false);
gunScript = gunObj.GetComponent<Gun>();
gunScript.SetPickedUp(true);
ShowCrosshair();
StartCoroutine(ContextHandler.GetInstance().HandleTimedMessage("Gun equipped!", 2));
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612140134im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Camera Controller default position? 0 Answers
Positioning a prefab correctly with Instantiate 3 Answers
How to make gameobject appear in center of camera irrespective of it's side?? 0 Answers
Scale gameobjects position depending on screen size like if it was a UI element 0 Answers
How to instantiate objects to mesh while restricting the spawn area on a single axis? 2 Answers