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 :slight_smile:

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;