Как запретить игроку стрелять снарядами после исчерпания боеприпасов

Я делаю игру, похожую на Space Shooter, и я хотел бы знать, как запретить игроку стрелять снарядами ПОСЛЕ того, как закончились боеприпасы.

Два приведенных ниже скрипта управляют движением и действиями моего игрока и уменьшают текущее количество снарядов каждый раз, когда игрок нажимает пробел.

Скрипт игрока

using UnityEngine;
using System.Collections;

public class Player : MonoBehaviour {

//Movement speed of Player sprite
public float Speed = 20.5f;
//GameObject to store the projectile object
public GameObject Projectile;

// Update is called once per frame
void Update () 
{
    //If left arrow key is pressed
    if (Input.GetKey (KeyCode.LeftArrow))
        //Move the player to the left
        transform.Translate (new Vector2 (-Speed * Time.deltaTime, 0f));

    //If right arrow key is pressed
    if (Input.GetKey (KeyCode.RightArrow))
        //Move the player to the right
        transform.Translate (new Vector2 (Speed * Time.deltaTime, 0f));

    //If the spacebar is pressed
    if (Input.GetKeyDown (KeyCode.Space)) 
    {
        //Instatiate a new projectile prefab 2 units above the Player sprite
        Instantiate (Projectile, transform.position + transform.up * 2, Quaternion.identity);
        //Find the game object with the tag "Projectile" and call the DecreaseProjectileCount() function from the ProjectileTracker script
        GameObject.FindGameObjectWithTag("Projectile").GetComponent<ProjectileTracker>().DecreaseProjectileCount();
    }
}

}

Скрипт ProjectileTracker

using UnityEngine;
using System.Collections;

public class ProjectileTracker : MonoBehaviour {

//Variable to store the current projectile count
public GameObject ProjectileRef;
//Set the current projectile count to be 8
int CurrentProjectileCount = 8;

//Function to decrease the current projectile count
public void DecreaseProjectileCount()
{
    //Decrease the current projectile count by 1
    CurrentProjectileCount--;
    //Print out the current projectile count
    ProjectileRef.GetComponent<TextMesh> ().text = CurrentProjecileCount.ToString ();
}

}

Любая форма помощи приветствуется!


person Darren Loke    schedule 06.08.2015    source источник
comment
Конечно, вы бы просто проверили текущее количество проектов (которое вы объявляете как int со значением 8). Если (CurrentProjectileCount › 0)   -  person MikeS159    schedule 06.08.2015


Ответы (1)


Как я бы лично это сделал:

ProjectileTracker tracker = GameObject.FindGameObjectWithTag("Projectile").GetComponent<ProjectileTracker>();

//If the spacebar is pressed
if (Input.GetKeyDown (KeyCode.Space) && tracker.ProjectileCount > 0) 
{
    //Instatiate a new projectile prefab 2 units above the Player sprite
    Instantiate (Projectile, transform.position + transform.up * 2, Quaternion.identity);
    //Find the game object with the tag "Projectile" and call the     DecreaseProjectileCount() function from the ProjectileTracker script
    tracker.ProjectileCount--;
}

...

using UnityEngine;
using System.Collections;

public class ProjectileTracker : MonoBehaviour {

    //Variable to store the current projectile count
    public GameObject ProjectileRef;
    //Set the current projectile count to be 8
    private int projectileCount = 8;

    public int ProjectileCount
    {
        get { return projectileCount; }
        set { SetProjectileCount(value); }
    }

    //Function to decrease the current projectile count
    public void SetProjectileCount(int value)
    {
        projectileCount = value;
        //Print out the current projectile count
        ProjectileRef.GetComponent<TextMesh> ().text = value.ToString();
    }
}
person maksymiuk    schedule 06.08.2015
comment
немного почистил, теперь должно быть получше - person maksymiuk; 06.08.2015