- Home /
Weapon Switching 3th Person
hi i got a question i got an 3th person character ant i want to weaponswitch whit animation
my guns are attatched to my right hand
the problem i got is that i can switch whit animation but as soon as i press 2 my hands already go in the positon from my pistol(second gun)
i tried to set a timer on it but it didend work
does someone of you now my awnser
thanks.
Edited : Code moved from comments to original post.
var Player : GameObject;
var AnimScript1 = "";
var AnimScript2 = "";
var PrimarySlot : GameObject;
var SecondarySlot : GameObject;
//private var UMP45 : boolean = false;
//private var M9 : boolean = false;
private var PrimarySelected : boolean = false;
private var SecondarySelected : boolean = false;
function Start () {
Player.GetComponent(AnimScript1).enabled = true;
Player.GetComponent(AnimScript2).enabled = false;
PrimarySelected = true; SecondarySelected = false;
PrimarySlot.gameObject.SetActiveRecursively(true);
SecondarySlot.gameObject.SetActiveRecursively(false);
animation["switch-down"].layer = 30;
animation["switch-up"].layer = 30;
animation["switch-down-ump3"].layer = 30;
animation["switch-up-ump2"].layer = 30;
}
function Update () {
if (Input.GetKey("1")) {
if (SecondarySelected) {
Player.animation.CrossFade("switch-down");
W1();
}
}
else if (Input.GetKey("2")) {
if (PrimarySelected) {
Player.animation.CrossFade("switch-up");
W2();
}
}
}
function W1 (){
animation.Stop();
//Player.animation.Play("switch-down");
//PrimarySlot.gameObject.SetActiveRecursively(false);
//SecondarySlot.gameObject.SetActiveRecursively(true);
yield WaitForSeconds(0.4);
animation.Stop("switch-down");
Player.animation.CrossFade("switch-up-ump2");
PrimarySlot.gameObject.SetActiveRecursively(true);
SecondarySlot.gameObject.SetActiveRecursively(false);
Player.GetComponent(AnimScript1).enabled = true;
Player.GetComponent(AnimScript2).enabled = false;
PrimarySelected = true;
SecondarySelected = false;
}
function W2 (){
animation.Stop();
Player.animation.CrossFade("switch-up");
//PrimarySlot.gameObject.SetActiveRecursively(true);
//SecondarySlot.gameObject.SetActiveRecursively(false);
yield WaitForSeconds(0.4);
Player.animation.Stop("switch-up");
PrimarySlot.gameObject.SetActiveRecursively(false);
SecondarySlot.gameObject.SetActiveRecursively(true);
Player.GetComponent(AnimScript1).enabled = false;
Player.GetComponent(AnimScript2).enabled = true;
PrimarySelected = false;
SecondarySelected = true;
}
this is what i got so far
var Player : GameObject; var AnimScript1 = ""; var AnimScript2 = ""; var PrimarySlot : GameObject; var SecondarySlot : GameObject;
//private var U$$anonymous$$P45 : boolean = false; //private var $$anonymous$$9 : boolean = false; private var PrimarySelected : boolean = false; private var SecondarySelected : boolean = false;
function Start () { Player.GetComponent(AnimScript1).enabled = true; Player.GetComponent(AnimScript2).enabled = false;
PrimarySelected = true; SecondarySelected = false;
PrimarySlot.gameObject.SetActiveRecursively(true); SecondarySlot.gameObject.SetActiveRecursively(false);
animation["switch-down"].layer = 30;
animation["switch-up"].layer = 30;
animation["switch-down-ump3"].layer = 30;
animation["switch-up-ump2"].layer = 30;
}
function Update () { if (Input.Get$$anonymous$$ey("1")) { if (SecondarySelected) { Player.animation.CrossFade("switch-down"); W1();
} } else if (Input.Get$$anonymous$$ey("2")) { if (PrimarySelected) { Player.animation.CrossFade("switch-up"); W2();
} } }
function W1 (){ animation.Stop(); //Player.animation.Play("switch-down"); //PrimarySlot.gameObject.SetActiveRecursively(false); //SecondarySlot.gameObject.SetActiveRecursively(true); yield WaitForSeconds(0.4); animation.Stop("switch-down"); Player.animation.CrossFade("switch-up-ump2"); PrimarySlot.gameObject.SetActiveRecursively(true); SecondarySlot.gameObject.SetActiveRecursively(false); Player.GetComponent(AnimScript1).enabled = true; Player.GetComponent(AnimScript2).enabled = false; PrimarySelected = true; SecondarySelected = false; }
function W2 (){ animation.Stop(); Player.animation.CrossFade("switch-up"); //PrimarySlot.gameObject.SetActiveRecursively(true); //SecondarySlot.gameObject.SetActiveRecursively(false); yield WaitForSeconds(0.4); Player.animation.Stop("switch-up"); PrimarySlot.gameObject.SetActiveRecursively(false); SecondarySlot.gameObject.SetActiveRecursively(true); Player.GetComponent(AnimScript1).enabled = false; Player.GetComponent(AnimScript2).enabled = true; PrimarySelected = false; SecondarySelected = true; }
Could you edit that to be a bit better formatted? All you need to do is put four spaces before each line.
@DutchWarfare12 : Just stumbled upon this thread. Edited your main post and added your code to it for you since this is your first topic/post on the site. Try formatting properly next time.
Your answer
![](https://koobas.hobune.stream/wayback/20220613071544im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
A node in a childnode? 1 Answer
Weapon Switching 2 Answers
RPG scripts 2 Answers
WeaponScript not working 0 Answers
How to add a third Variable? 2 Answers