- Home /
FPS Weapon Switch Script
Hi i am currently working on a FPS and i was wondering how i would do a weapon switch animation where when i press "2" the gun will move down and the pistol will come out any ideas? i was testing this script but i cant see it slowly go down???
var GunUp : Vector3;
var GunDown : Vector3;
var GunMoveSpeed = 0.5;
function Start ()
{
transform.localPosition = GunUp;
}
function Update ()
{
if(Input.GetKey("2")){
transform.localPosition = Vector3.Lerp(transform.localPosition,GunDown,GunMoveSpeed*Time.deltaTime);
}
if(!Input.GetKey("2")){
transform.localPosition = Vector3.Lerp(transform.localPosition,GunUp,GunMoveSpeed*Time.deltaTime);
}
}
Well in your code it goes in first if statemen only while you hold key 2. You can call coroutine and in it move the object, or just create a regular animation to move it and not through code
The followinf statement doesn't make sense for what you are trying to achieve, please read the manual for user input.
if(!Input.Get$$anonymous$$ey("2")){
transform.localPosition = Vector3.Lerp(transform.localPosition,GunUp,Gun$$anonymous$$oveSpeed*Time.deltaTime);
}
While you don't hold the key 2, this line of code will be called, I don't think you want this to happen
Answer by rhbrr5hrfgdfgw · Jun 19, 2015 at 02:38 PM
I don't understand do you want this to work with two guns? if you do:(assuming they you want both weapons to end up in the exact vector
Should work
var GunUp : Vector3;
var Gun : GameObject;
var pistol : GameObject;
var GunDown : Vector3;
var GunMoveSpeed = 0.5;
function Start ()
{
transform.localPosition = GunUp;
}
function switchGuns(var gun : GameObject, var pistol : GameObject){
if(Input.GetKeyUp(KeyCode.Alpha1)){
pistol.transform.localPosition = Vector3.Lerp(pistol.transform.localPosition,GunDown,GunMoveSpeed*Time.deltaTime);
gun.transform.localPosition = Vector3.Lerp(gun.transform.localPosition,GunUp,GunMoveSpeed*Time.deltaTime);
}
if(Input.GetKeyUp(KeyCode.Alpha2)){
gun.transform.localPosition = Vector3.Lerp(gun.transform.localPosition,GunDown,GunMoveSpeed*Time.deltaTime);
pistol .transform.localPosition = Vector3.Lerp(pistol.transform.localPosition,GunUp,GunMoveSpeed*Time.deltaTime);
}
}
function Update ()
{
switchGuns(Gun,pistol);//Put in editor the gameobjects
}
Your answer
