top of page
Search

First Person Shooter Updates

  • Writer: Keshav Batra
    Keshav Batra
  • Mar 4, 2021
  • 1 min read

This week I had a whole bunch of things done with my first person game. The first thing I did was I added a health bar to limit the amount of times I can get hit. Then I programmed a way for me to start over from the beginning after running out of health. I even added a way for the enemy to disappear after a certain amount of times you shoot them. After that I added some function to where I could switch to different weapons. Finally I added a reload animation for when I run out of bullets. The end result is this:




Here's the code for the health bar:


{


public Slider slider;

public Gradient gradient;

public Image fill;


public void SetMaxHealth(int health)

{

slider.maxValue = health;

slider.value = health;


fill.color = gradient.Evaluate(1f);

}


public void SetHealth(int Health)

{

slider.value = Health;


fill.color = gradient.Evaluate(slider.normalizedValue);

}

}

Now here's the code for the weapon switch:


{

public int selectedWeapon=0;

void Start () {

SelectWeapon();

}


// Update is called once per frame

void Update() {


int previousSelectedWeapon = selectedWeapon;


if (Input.GetAxis("Mouse ScrollWheel") > 0)

{

if (selectedWeapon >= transform.childCount - 1)

selectedWeapon = 0;

else

selectedWeapon++;

}

if (Input.GetAxis("Mouse ScrollWheel") < 0)

{

if (selectedWeapon <= 0)

selectedWeapon = transform.childCount-1;

else

selectedWeapon--;

}


if (Input.GetKeyDown(KeyCode.Alpha1))

{

selectedWeapon = 0;

}


if (Input.GetKeyDown(KeyCode.Alpha2)&&transform.childCount>=2)

{

selectedWeapon = 1;

}


if (Input.GetKeyDown(KeyCode.Alpha3) && transform.childCount >=3)

{

selectedWeapon = 2;

}


if (Input.GetKeyDown(KeyCode.Alpha4) && transform.childCount >=4)

{

selectedWeapon = 3;

}




if (previousSelectedWeapon != selectedWeapon)

{

SelectWeapon();

}

}


void SelectWeapon()

{

int i = 0;

foreach (Transform weapon in transform)

{

if (i== selectedWeapon)

weapon.gameObject.SetActive(true);

else

weapon.gameObject.SetActive(false);

i++;

}

}

}





 
 
 

Comments


bottom of page