Hey! Sorry for posting so soon, but I have a problem. I’m trying to create a weapon system. I can make weapons disappear and appear when selected, but it’s pretty rapid. I’m trying to make them appear and disappear using arming and disarming animations. That’s ok to do too, but the problem is that I want to make the new selected weapon detect if the previous weapon has finished the disarming animation and than play it’s arm animation.
Here’s my weapon selector code so far (it’s actually a camera code with weapon stuff):
var rotation = 0.0;
var current_pitch = 0.0;
static var current_weapon : int = 0;
var localcurrent_weapon : int = 0;
var weapon1_current : boolean = false;
var weapon2_current : boolean = false;
function Update () {
//Camera stuff
current_pitch -= Input.GetAxis("Mouse Y") * 7;
current_pitch = Mathf.Clamp(current_pitch, -80, 80);
transform.localEulerAngles = Vector3(current_pitch, 0, 0);
//weapon selection stuff
weapon1_current = GameObject.Find("1").GetComponent(weaponscode).isDisarmed;
weapon2_current = GameObject.Find("2").GetComponent(weaponscode).isDisarmed;
if (Input.GetKeyDown("1")) {
localcurrent_weapon = 1;
}
else if (Input.GetKeyDown("2")) {
localcurrent_weapon = 2;
}
if(weapon1_current == true localcurrent_weapon == 1)
{
current_weapon = 1;
}
if(weapon2_current == true localcurrent_weapon == 2)
{
current_weapon = 2;
}
}
and this is my weapons’ code:
static var isCurrent : boolean = false;
static var isDisarmed : boolean = false;
var weaponnumber : int = 0;
function Update () {
if(transform.parent.GetComponent(CamControl_SP).current_weapon == weaponnumber)
{
isCurrent = true;
if(animation.isPlaying == false)
{
isDisarmed = false;
}
}
else
{
if(animation.isPlaying == false)
{
isDisarmed = true;
}
isCurrent = false;
}
if(isCurrent == true isDisarmed == true)
{
Arm();
}
else {
if(isCurrent == false isDisarmed == false)
{
Disarm();
}
}
}
function Arm () {
animation.Play ("arm");
}
function Disarm () {
animation.Play ("disarm");
}
Where is my mistake ?
try the string version of this.
Tried it. Still nothing happens, the weapons just don’t bother to arm/disarm…
function Update ()
{
// lets assume that both weaponas are in back so this weapon (#1) isCurrent = false and isDisarmed = false
if(transform.parent.GetComponent(CamControl_SP).current_weapon == weaponnumber)
{
// user pressed 1 so we set our isCurrent to true:
isCurrent = true;
if(animation.isPlaying == false)
{ // no animation is plaing so we won't come here.
isDisarmed = false;
}
}
else
{ // and since we handled situation in previos if we skip this else
if(animation.isPlaying == false)
{
isDisarmed = true;
}
isCurrent = false;
}
if(isCurrent == true isDisarmed == true)
{ // is current, yes, but isDisarmed is false so don't go here.
Arm();
}
else
{ // so we go here.
if(isCurrent == false isDisarmed == false)
{ // isDisarmed == false is true, but since isCurrent is true, don't go here either.
Disarm();
}
}
// and we are done.
}
Still don’t know why you want to poll in every weapon’s update() what happened in script attached to camera.
Well I just don’t know what’s the right thing to do here. I’m pretty confused what to do. In my game I’ll have only three weapon slots and I want the other weapons to know if they can be armed, so I made the camera script check (for now there’s only two weapons) what’s the state of the weapons, but as you all can see it’s pretty unsuccessful. If somebody can offer a better way to do all this, I would really appreciate it!
I would say that you should probably just keep the animation functions on the weapon script and keep track of what is selected entirely in the other script. The logic is quite complicated at the moment.
not tested. May contain traces of bugs
WeaponManager.js
private var isDoingSomething : boolean = false;
private var CurrentWeapon : int = 0; // start empty handed
function Update()
{
if(!isDoingSomething)
{ // is not doing anything a the moment so we can arm/disarm weapons
if(Input.GetKeyDown("1")
SetWeapon(1);
if(Input.GetKeyDown("2")
SetWeapon(2);
if(Input.GetKeyDown("3")
SetWeapon(3);
}
}
private function SetWeapon(number : int)
{
// Don't let change weapon if we are already changing
isDoingSomething = true;
waitTime : float = 0.0;
if(CurrentWeapon)
{ //Has weapon in hand, disarm it.
waitTime = return GameObject.Find("Weapon"+CurrentWeapon).GetComponent(weaponscode).Disarm();
}
if(number == CurrentWeapon)
{ // pressed button for current weapon so we just disarm it and stay empty handed OR has w
CurrentWeapon = 0;
}
else
{ // Lets change weapon to something else
CurrentWeapon = number;
}
// wait for disarm animation to finnish
yield WaitForSeconds(waitTime);
if(CurrentWeapon) //we have weapon to arm
{
// Tell the new weapon to play arm-animation and ask how long it is
waitTime = GameObject.Find("Weapon+"number).GetComponent(weaponscode).Arm();
// wait for arm animation to finnish
yield WaitForSeconds(waitTime);
}
// Done swapping weapons.
isDoingSomething = false;
}
and weaponscode.js
/* Weapons are dumb. All they can do is play their animations. They don't know anything else. */
function Arm()
{
animation.Play ("arm");
// tell whoever called us how long this animation is.
return animation["arm"].length;
}
function Disarm()
{
animation.Play ("disarm");
// tell whoever called us how long this animation is.
return animation["disarm"].length;
}
Finding weapons is bit too blunt for my taste but for demo purpose I kept it short. Should have error check if object or component is found.