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 Zacrye · Sep 13, 2013 at 02:46 AM · transformrotate objectweaponequipcusm rotation

Why is it that when I equip weapons, they rotate in different ways depending on which way I[my character-player] am[is] looking?

Everytime I equip a weapon or the model of my weapon using scripting, I keep running into the same issue. Here's an example: I have a pickaxe, I can click on it in my inventory and send it to the equipment slot, it goes to the right hand like it's supposed to, but it never has the proper rotation. Here is a snip of my code... the code that's supposed to spawn and give proper rotation to the weapon relative to the direction of my character

 for(var x = 0; x < playerInventory.Count; x++){
         GUILayout.BeginHorizontal();
         if(GUILayout.Button(playerInventory[x].icon) && playerInventory[x].itemType == ItemClass.ItemType.Weapon && equipped == false)
         {
             equippedItem = playerInventory[x];
             var weapon : Transform = Instantiate(playerInventory[x].itemPrefab,rightSlot.position,Quaternion.identity);
             weapon.parent = rightSlot;
             playerInventory.RemoveAt(x);
             return;
         
         }

if anyone knows or has experienced my problem and can help, it'd be much appreciated. Thanks.

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Zacrye · Sep 13, 2013 at 11:34 PM

Hey guys! Through much trial and error I have found the solution and will post it here just in case anyone else has this awful problem!

     for(var x = 0; x < playerInventory.Count; x++){
         GUILayout.BeginHorizontal();
         if(GUILayout.Button(playerInventory[x].icon) && playerInventory[x].itemType == ItemClass.ItemType.Weapon && equipped == false)
         {
             equippedItem = playerInventory[x];
         
             var weapon : GameObject;
             weapon = Resources.Load("WeaponFabs/" + playerInventory[x].name) as GameObject;
             
             print ("WeaponFabs/" + playerInventory[x].name);
             var rightHand : Transform = GameObject.Find("Right_Slot").transform;
             
             var rightSlot : Quaternion = GameObject.Find("Character").transform.localRotation;
             
             var spawn_right_weapon = Instantiate (weapon,rightHand.position,Quaternion.identity);
             
             //spawn_right_weapon.transform.eularAngles = rightHand;
             spawn_right_weapon.transform.localRotation = rightSlot;
             spawn_right_weapon.transform.parent = rightHand;
             
             //var weapon : Transform = Instantiate(playerInventory[x].itemPrefab,rightSlot.position,Quaternion.identity);
             //weapon.transform.parent = rightSlot;
             playerInventory.RemoveAt(x);
             return;
         
         }

don't mind the notes[the '//' for those who do not know and or are very new], they were there as a result of me testing multiple ways of doing this. So, first off, what is happening here is that by basically clicking the icon for the item in my inventory it get sent to the equip panel by checking if I clicked the icon, (playerInventory[x].icon), then if it's of the weapon type, and if I already have something 'equipped'. Next it does the sending of the icon from point A: the inventory to point B: the equip button/section with equippedItem = playerInventory[x];. Next it creates the weapon variable and loads the corresponding weapon from the clicked icon from 'Assests/Resources/WeaponFabs' [note it won't work, the Resources.Load("WeaponFabs/" + playerInventory[x].name) if you do not have a resources folder and if the name of the prefab doesn't match the name of the weapon/item you assigned the icon to]. Now the most important parts. THIS IS HOW YOU SOLVE THE ROTATING PROBLEM. FIRST: var rightHand : Transform = GameObject.Find("Right_Slot").transform; gets the current position of the Object in the scene named Right_Slot, i.e. this will tell your prefab weapon WHERE to spawn[note: I parented the right-slot,an empty game object, to the bone in the right hand, not doing this for some reason and trying to spawn it on the right hand, resulted in it spawning in not the right area what-so-ever]. var rightSlot : Quaternion = GameObject.Find("Character").transform.localRotation; gets the current ROTATION of the the main character are your bones are attached/parented to and basically allows the weapon to know which way to point. The rest of the code just tells where the variable 'weapon' should go and how it should rotate, after attaining said info from the previous lines. Well, that was a mouth full. I really hope this helps someone if they're looking to fixing their rotating weapon problems! Good luck, guys--and girls!

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

15 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

Related Questions

How do I set and get local rotation of objects 1 Answer

Rotating an already rotated object. 1 Answer

How to move child objects perpendicular to parent object's rotation? 2 Answers

Set parent of an instantiated object where the object being instantiated is a transform 1 Answer

really annoying rotation problem. 0 Answers


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