• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by christoffermagnusson · Oct 02, 2017 at 07:36 PM · camerapositioninginstantiate prefab

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)

alt text

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));
     }

gun-problems.png (477.1 kB)
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

153 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Scale gameobjects position depending on screen size like if it was a UI element 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

How to instantiate objects to mesh while restricting the spawn area on a single axis? 2 Answers

Prefabs aren't instantiating where they should be 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges