Switch Weapons and Reload Animation Sequence

Hi everybody, i just started learning Unity and happily completed the basic tutorials and the FPS tutorial also, but i have a problem that i haven’t been able to figure out. How can you create an switch weapon animation and a reload animation?. Please if anyone has a suggestion for this i really apreciatte it.

Thanks a lot for your help. (i hope my english is not too bad :wink: )

I understand you just fine. :slight_smile: I haven’t got there yet but I am planning on animating the arm(s) and machine gun in XSI. Then you just play the animation when the guy presses reload, complete with cartridge falling. Then I swap in your normal “idle” animation.

To switch a weapon you could have an animation that “swings out” an M16 and swings back in a shotgun.

Hey.

(Your english is just fine :slight_smile: )

If you have a modelling program, you’d best bet would be to animate the gun in there, and then play back that animation in unity.

If you don’t, you could “code the animation”, for instance by having a script move the weapon lower and lower, untill it is out of view, then replace it with a different model, and start moving the weapon higher and higher again.

Good luck, Lucas

Thanks a lot for the help, am using xsi actually… does any of you have an example? i would change 3d assets for code! Thanks a lot again :slight_smile:

You would play an animation on change. If you looked at the old FPS, you could use that as a base and change the script to play the animation for each weapon. I’m not anywhere remotely a good coder but that is how I’m planning on doing it.

Use the example projects and tuts for help. It has helped me to figure some things out.

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;
}

}
}

when u are drawing a weapon u can make it withoud a script, in start u pick ur animation, set it up on the object, set it to play automaticly(not looped ofc) and 1st animation that plays to be the draw one, i use that and works perfect :slight_smile: