【问题标题】:How to prevent player from shooting projectiles after ammo depletion如何防止玩家在弹药耗尽后发射弹丸
【发布时间】:2015-08-06 23:51:56
【问题描述】:

我正在制作一款类似于 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 ();
}

}

感谢任何形式的帮助!

【问题讨论】:

  • 当然,您只需检查当前项目计数(您将其声明为值为 8 的 int)。 If(CurrentProjectileCount > 0)

标签: c# unity3d projectile


【解决方案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();
    }
}

【讨论】:

  • 清理了一下,现在应该好多了
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多