Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 naruto · Jun 18, 2011 at 02:17 PM · weaponmanager

weapon manager

iv been working on a weapons manager and i have it spawning and switching weapons.... but im having a hard time with rotation and parenting the weapons, what i want is to make the weapon a child of "WeaponPoint" and as for the rotation i want it to use the forward faceing of "WeaponPoint" if anyone could help i would be heartful. heres my code

 var WeaponsInInventory : GameObject[];
   
 var EquippedWeapon : int;
 
 var WeaponPoint : Transform;
 
 function Update () {
 
 // ***********CODE TO SCROLL THROUGH WEAPONS *****************************
 
 if (Input.GetAxis("Mouse ScrollWheel")  > 0){
          
          EquippedWeapon++;
          EquipWeapon();
          Debug.Log(EquipedWeapon);
  }
  
 if (Input.GetAxis("Mouse ScrollWheel")  < 0){
          
          EquippedWeapon--;
          EquipWeapon();
          
          Debug.Log(EquipedWeapon);
         
      }
      
 if (EquippedWeapon < 0){
 
 EquipedWeapon = WeaponsInInventory.length; // if we try to go into  negetive numbers loop to the the last weapon in our inventory.
 
       EquipWeapon();
     
 }
 if (EquippedWeapon > WeaponsInInventory.length - 1){
 
 EquipedWeapon = 0; // if we try to go into a number greater than the number of weapons in our inventory loop to the first.
 
           EquipWeapon();
      }
 
 }
 
 function EquipWeapon() {
 
  Destroy (WeaponsInInventory[EquipedWeapon]); // destroy the weapon we are holding before  spawning our new weapon
     
      Instantiate(WeaponsInInventory[EquipedWeapon], GameObject.Find("WeaponPoint").transform.position, Quaternion.identity); // spawn our waeapon 
      Debug.Log(WeaponsInInventory[EquipedWeapon] + "was spawned");
      WeaponsInInventory[EquipedWeapon]transform.parent = GameObject.Find("WeaponPoint").transform.position; // make our weapon a child of our weapon spwan point
    
      
 }

P.S im still new to programing and my script is probably way off so try to go easy on me :)

Comment
Add comment · Show 7
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 Marnix · Jun 18, 2011 at 02:38 PM 0
Share

@naruto: Could you please edit your code formatting? This is pretty unreadable. You just pressed the 0101011 button, but you need to select your full script code first.

avatar image naruto · Jun 18, 2011 at 02:43 PM 0
Share

sorry about that it should be fixed

avatar image Marnix · Jun 18, 2011 at 03:00 PM 0
Share

What is exactly your question?

avatar image naruto · Jun 18, 2011 at 03:04 PM 0
Share

how to parent the weapon to "WeaponPoint" and as for the rotation i want to use the forward faceing of "WeaponPoint"

avatar image naruto · Jun 18, 2011 at 04:47 PM 0
Share

i got it to become a child to weaponpoint by addint this code to my weapon

function Awake (){

gameObject.transform.parent = GameObject.Find("WeaponPoint").transform;

}

but the weapon is always facing global x how do i go about finding the direction im facing and aply that to my weapon?

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Marnix · Jun 18, 2011 at 03:12 PM

Things I see on your code now:

 if (EquippedWeapon < 0)
 {
     // if we try to go into  negetive numbers loop to the the last weapon in our inventory.
     EquipedWeapon = WeaponsInInventory.length; 
     EquipWeapon();
 }

This part will give you an error, because you have to set it to length-1. Also, have a good look at equipped and equiped. Spelling errors are very common in computer programming and do not always seem to be an error, but they are.

To your question about rotation and parenting:
Your line of code for parenting needs an extra dot. Don't know if that is your compiler error, but it's wrong now. And you don't need the position to parent. You are parenting transforms to each other.

WeaponsInventory[EquippedWeapon].transform.parent = GameObject.Find("WeaponPoint").transform;

But this doesn't put your new weapon to the new position. Parenting in Unity is not setting the position to the right place. So what do we do after parenting? Set the position to a correct offset from the parent. So we do:

 WeaponsInventory[EquippedWeapon].transform.localPosition = Vector3.zero;

Or any other offset position that makes it go on your hand exactly. Please read more about parenting here: http://unity3d.com/support/documentation/Components/class-Transform.html .

If the forward of your WeaponPoint is ok, you don't have to change the rotation of your weapon. Just set it to 0:

 WeaponsInventory[EquippedWeapon].transform = Quaternion.identity;
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

2 People are following this question.

avatar image avatar image

Related Questions

gun manager how to 2 Answers

find the angle to rotate the gun to face mouse point with its barrel 1 Answer

Weapon Switch System for Gear VR Controller 0 Answers

HDRP Camera weapon layer 1 Answer

How do I stop my weapon from firing while reloading? 2 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