For Weapon Swapping: If we want to add more weapons, we must edit...

For Weapon Swapping: If we want to add more weapons, we must edit WeaponSwapScript.cs to accomodate for number button weapon swapping.
parent 23c5b916
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
//We 'aquired' some code from our awesome director Andrew Conn, thanks buddy!
public class AutoGunScript : MonoBehaviour
{
public float damage = 7f; // this used to be 10f from GunScript
public float range = 100f; // this used to be 100f from GunScript
public float fireRate = 20f; // this used to be 15f from GunScript
public Camera fpsCam;
public ParticleSystem muzzleFlash;
private float nextTimeToFire = 0f;
// Update is called once per frame
void Update()
{
if (Input.GetButton("Fire1") && Time.time >= nextTimeToFire)
{
nextTimeToFire = Time.time + 1f / fireRate;
Shoot();
}
}
// This makes sure that the gun is shot! (Unchanged from GunScript)
void Shoot()
{
muzzleFlash.Play();
RaycastHit hit;
if (Physics.Raycast(fpsCam.transform.position, fpsCam.transform.forward, out hit, range))
{
Debug.Log(hit.transform.name);
Enemy enemy = hit.transform.GetComponent<Enemy>();
if (enemy != null)
{
enemy.TakeDamage(damage);
}
}
}
}
using System;
using UnityEngine;
//We 'aquired' some code from our awsome director Andrew Conn, thanks buddy!
public class SemiAutoGunScript : MonoBehaviour
{
public float damage = 15f; // this used to be 10f from GunScript
public float range = 100f; // this used to be 100f from GunScript
public float fireRate = 10f; // this used to be 15f from GunScript
public Camera fpsCam;
public ParticleSystem muzzleFlash;
private float nextTimeToFire = 0f;
// Update is called once per frame
void Update()
{
if (Input.GetButton("Fire1") && Time.time >= nextTimeToFire)
{
nextTimeToFire = Time.time + 1f / fireRate;
Shoot();
}
}
// This makes sure the gun shoots! (Unchaged from GunScript)
void Shoot()
{
muzzleFlash.Play();
RaycastHit hit;
if (Physics.Raycast(fpsCam.transform.position, fpsCam.transform.forward, out hit, range))
{
Debug.Log(hit.transform.name);
Enemy enemy = hit.transform.GetComponent<Enemy>();
if (enemy != null)
{
enemy.TakeDamage(damage);
}
}
}
}
using System;
using UnityEngine;
/*
* This WeaponSwapScript makes sure that, if the player wants to swap weapons,
* they can swap weapons!
*
* WARNING: If we want to add more weapons, we have to modify this script to
* accomodate for swapping weapons with the number buttons. Currently, it can
* swap through 3 weapons.
*/
public class WeaponSwapScript : MonoBehaviour
{
public int selectedWeapon = 0;
// Start makes sure a weapon is selected at the start of a level
void Start()
{
SelectWeapon();
}
// Update is called once per frame.
// Update checks to see if either:
// -the scroll wheel is changing what weapon is active
// -the number buttons (not the num pad) are changing what weapon is active
void Update()
{
//This is used at the bottom of Update to check and make sure that the
//current weapon is the same as the newly selected weapon.
//If they're different, then the current weapon will update.
int previousSelectedWeapon = selectedWeapon;
//When the scroll wheel goes up.
if (Input.GetAxis("Mouse ScrollWheel") > 0f)
{
//If the selected weapon is already at the top of list,
//then it'll wrap around to the bottom of the list. Else, keep
//going up the list of weapons.
if (selectedWeapon >= transform.childCount - 1)
selectedWeapon = 0;
else
selectedWeapon++;
}
//When the scroll wheel goes down.
if (Input.GetAxis("Mouse ScrollWheel") < 0)
{
//If the selected weaopn is already at the bottom of the list,
//then it'll wrap around to the top of the list. Else, keep
//going down the list of weapons.
if (selectedWeapon <= 0)
selectedWeapon = transform.childCount - 1;
else
selectedWeapon--;
}
//These will alllow the nubmer buttons to change what weapon you're using.
//WARNING: If we plan to add more weapons and want them to be able to swap with them
//in game, then we must add to this!!!!
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;
}
//This calls SelectWeapon to change weapons
if (previousSelectedWeapon != selectedWeapon)
{
SelectWeapon();
}
}
//This is what actually changes the weapons. It loops through all of the weapons
//and either toggles the weapon active or not active
void SelectWeapon()
{
int x = 0; //the 'iterator' of the foreach loop
foreach (Transform weapon in transform)
{
if (x == selectedWeapon) //if this is the weapon the player wants, then it becomes true.
weapon.gameObject.SetActive(true);
else //if this weapon is NOT what the player wants, then it becomes false.
weapon.gameObject.SetActive(false);
x++;
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment