Hi there!
Right now I’ve got a Script that consists mainly of an Array in which I plan to store all my Weapons.
What I’m trying to do is to let the player swap between all weapons in the Array, just by pressing LeftShift.
I managed to get the code working by myself until the very moment in which Weapons are forced to " switch back"… like: I’m at the end of the Array, when Shift is pressed again it’s time to go back to the start.
For some reasons, the code you kindly wrote me doesn’t get the job done either.
I’m now posting you my script, maybe the problem is inherent to logic itself… I’ll restate what I’d like to do: write a script that handles all weapons in one array, gives the player the chance to swap between them ( later this will be done by power ups) and also gets the current weapon’s stats and use them when pushed to shoot.
Now that makes sense to me, but maybe it’s not program-minded… or maybe I’m just stupid, who knows? maybe you know it!
using UnityEngine;
using System.Collections;
public class WeaponManager : MonoBehaviour {
public float hitDamage; // The Damage we want do deal with it
public float fireRate; // The Speed at wich we shoot
public float projectileSpeed; // This is the Speed of the bullet
public GameObject currentWeapon; // Let's see.
public float nextFire = 0.0f;// The delay till the next shot once fireRate is added.
public GameObject [] weaponsArray = new GameObject[2];// This is our Array to store all the things. At the time I'm writing, I've got only 2 weapons as test.
// I'll use Instructor and not code to specify what GameObject is element zero, one and so on...
private int currentWeaponIndex = 0; // THIS INT IS NEEDED to keep track of the current weapon we are on it.
// Use this for initialization
void Start () {
currentWeapon = weaponsArray[currentWeaponIndex]; // The weapon with which you start. Linking it with currentWeaponIndex let's us change it dinamically!
}
// Update is called once per frame
void Update () {
//Cheap Projectile Speed variable
//currentWeapon.transform.Translate(Vector3.right * projectileSpeed * Time.deltaTime, Space.World); // it's working only if you attach this to the weapon itself. Maybe it's the same for Shooting...
//Swap Weapon, just for testing sake
if (Input.GetKeyDown(KeyCode.LeftShift)){
currentWeaponIndex ++; // i.e. add 1 to our integer. Lets us swap between every element of our Array.
if (currentWeaponIndex >= weaponsArray.Length){ // If we are trying to swap to an integer that is not in our Array, i.e. an integer outside our number's lenght.
currentWeaponIndex = 0; // Current weapon index goes back to zero, we " loop".
currentWeapon = weaponsArray[currentWeaponIndex]; // your weapon equals to the one that your index says... i.e. zero right now.
}
}
//Shoot Sample
if (Input.GetKeyDown(KeyCode.Space) && Time.time > nextFire){
nextFire = Time.time + fireRate;
currentWeapon.transform.position = new Vector3(PlayerV31.x, PlayerV31.y, PlayerV31.z);
Instantiate(currentWeapon);
}
}
}
P.S.: at this time, instantiate works but it doesn’t get the fire rate of the current weapon ( I know I haven’t said him to do so but fact is I’m a bit confused on how to say it) AND the weapon swap doesn’t work.
Thanks a lot for your time!