public bool ChangeWeapon(){
// ********************************************************************
// Free stuff !! OK Jorge, I’ll just move that up in my hectic development. lol !
//
// Here is a quick example of one way to do it. I did the weapon swap with Unity. I am
// going to do a badass smokin cartridge swap with XSI.
// My apologies for the lack of any finesse or if this isn’t how it “really” should be done.
//
// You need to have a “GunMount” GameObject parented under your MainCamera. This hunk of
// code manipulates the GunMount.
//
// GunMount will be the parent to your 1. gun and 2. your hand.
//
// The concept here is to rotate and move the GunMount (with the arm and the gun)
// off the screen, change the weapon, and then bring it back with the reverse
// movement.
//
// Add in your weapon.
// Call the ChangeWeapon().
//
// Now ! We, the programmer types want a hand pistol … we are sick of programmer
// art !
//
// Hope This Helps. Randy
// ********************************************************************
if (swapWeapon == changeWeaponState.changeNone) // Just do it once !
{
swapWeapon = changeWeaponState.changeOut;
}
return true;
}
enum changeWeaponState // Available states for changing weapons.
{
changeNone,
changeOut,
changeIn
}
private changeWeaponState swapWeapon = changeWeaponState.changeNone;
public override void Update()
{
// ********************************************************************
// UPDATE Person
// ********************************************************************
base.Update(); // BaseObject
int swapSpeed = 4; // speed to swap the weapons out
if (swapWeapon==changeWeaponState.changeOut)
{
Transform GunMount = GetPlayer().transform.Find(“MainCamera/GunMount”);
Quaternion DesiredRotation = Quaternion.identity;
Vector3 DesiredPosition = GunMount.localPosition + new Vector3(0.15f,0,0);
DesiredRotation *= Quaternion.Euler(0, 223, 0); // 223 is the angle to finish on
GunMount.localRotation = Quaternion.Slerp(GunMount.localRotation, DesiredRotation, Time.deltaTime * swapSpeed);
GunMount.localPosition = Vector3.Slerp(GunMount.localPosition, DesiredPosition, Time.deltaTime * swapSpeed);
if (Quaternion.Angle(GunMount.localRotation, DesiredRotation) < 3)
{
swapWeapon=changeWeaponState.changeIn;
// Destroy() your 1st weapon here
// Instantiate and parent in the new weapon here
}
}
else if (swapWeapon == changeWeaponState.changeIn)
{
Transform GunMount = GetPlayer().transform.Find(“MainCamera/GunMount”);
Quaternion DesiredRotation = Quaternion.identity;
Vector3 DesiredPosition = GunMount.localPosition + new Vector3(0.15f, 0, 0);
DesiredRotation *= Quaternion.Euler(0, 180, 0); // 223 is the angle to swing off screen
GunMount.localRotation = Quaternion.Slerp(GunMount.localRotation, DesiredRotation, Time.deltaTime * swapSpeed);
GunMount.localPosition = Vector3.Slerp(GunMount.localPosition, new Vector3(0.1275735f, -0.2144322f, 0.5727234f), Time.deltaTime * swapSpeed);
if (Quaternion.Angle(GunMount.localRotation, DesiredRotation) < 3)
{
swapWeapon = changeWeaponState.changeNone;
}
}
}