Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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 /
avatar image
0
Question by Exploder1010 · Dec 12, 2014 at 03:42 AM · instantiateweaponoffset

Help with a different approach to instantiating weapons?

http://answers.unity3d.com/questions/249940/making-instantiated-weapon-a-child-of-the-player.html

My problem is the same as in the above thread (essentially how to offset an instantiated weapon relative to the parent player), however, I was wondering if a different approach that might make it easier for me would be possible. I plan on having a relatively large number of weapons in my game, and it so i may have to have multiple different offset positions to correct the position and rotation of each. For this reason I am looking for an alternative to using empty gameobjects, and creating one for each, or at least many, different weapon. What I thought would work was setting the position and rotation variables on the actual weapon prefab, then passing them into the weapon equip script like this:

     Transform item =(Transform)Instantiate(itemPrefab, transform.position, transform.rotation);

     //attach weapon to player
     item.parent = transform;
 
     Vector3 position = new Vector3(itemPrefab.transform.position.x, itemPrefab.transform.position.y, itemPrefab.transform.position.z);
     Quaternion rotation = transform.rotation;
     rotation.eulerAngles = new Vector3(rotation.eulerAngles.x, rotation.eulerAngles.y + itemPrefab.transform.rotation.y, rotation.eulerAngles.z);

     item.transform.position += position;
     item.transform.rotation = Quaternion.Euler(0,itemPrefab.transform.rotation.y, 0);

However, the offset position does not take into account the rotation of the player, so if I were to say offset the weapon by 1.0f forward, it would spawn at the coordinate of the player + 1.0f, which could be in front of the player, to the left, behind, etc... depending on where the player is looking when the weapon is instatiated.

Is there a way around this? Would it be easier and simpler to just use empty gameobjects? One of my primary concerns is that if I parent like 30 different gameobjects to the player, anticipating every possible location I would want a weapon to be, that it might eat up preformance?

Thanks for any help with this question! I'll post more code if this is not enough.

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by VesuvianPrime · Dec 12, 2014 at 03:44 AM

You're very close to getting this working, you just need to set the localPosition and localRotation of the weapon transform instead of the world position and rotation.

Comment
Add comment · Show 1 · Share
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
avatar image Exploder1010 · Dec 12, 2014 at 04:11 AM 0
Share

Thanks, it works perfectly!

If anyone has the same problem in the future, here is my code:

  Transform item =(Transform)Instantiate(itemPrefab, itemPosition.transform.position, itemPosition.transform.rotation);
  
  item.parent = itemPosition.transform;    
 
  item.transform.localPosition += itemPrefab.position;
  item.transform.localRotation = itemPrefab.rotation;
 

"itemPosition" is an empty gameObject at the position of the camera. I knew I said I didn't want to use gameObjects, but with this method I only have to use one.

avatar image
0

Answer by Exploder1010 · Dec 12, 2014 at 09:59 AM

Thanks, it works perfectly!

If anyone has the same problem in the future, here is my code:

     Transform item =(Transform)Instantiate(itemPrefab, itemPosition.transform.position, itemPosition.transform.rotation);
     
     item.parent = itemPosition.transform;    

     item.transform.localPosition += itemPrefab.position;
     item.transform.localRotation = itemPrefab.rotation;

"itemPosition" is an empty gameObject at the position of the camera, and on the equipped weapons layer (so that I can set a culling mask to a secondary camera to prevent the weapon from clipping through walls). I knew I said I didn't want to use gameObjects, but with this method I only have to use one.

Comment
Add comment · Share
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

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.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Checking if object intersects? 1 Answer

Instantiating an object via its center (as opposed to its pivot point) 2 Answers

Stop Children of Instantiated Parent From Offsetting After Changing Position 0 Answers

How to Add Y Axis Offset to transform.localPosition on an Instantiated Prefab? 2 Answers

Instantiate New Gun 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges