Unity2D:玩家收集10分后,如何让spawn对象逐渐加快?

时间:2016-08-11 23:16:33

标签: c# unity3d spawn

所以我想知道是否有一种方法可以让玩家(你的用户)收集10分后逐渐移动/加快生成的对象。当玩家收集另外10分等等时更快?等等?

这是我的移动脚本附加到我的对象中产生的:

public class Movement : MonoBehaviour
  {
   public static int movespeed = 20;
   public Vector3 userDirection = Vector3.right;

public void Update()
 {
    transform.Translate(userDirection * movespeed * Time.deltaTime); 
 }
}

这是我的分数附加到我的播放器

public int Score;
public Text ScoreText;

void Start ()
{
    Score = 0;
    SetScoreText ();
}

void OnTriggerEnter2D(Collider2D other)
{
    if (other.gameObject.CompareTag ("Pick Up")) 
    {
        other.gameObject.SetActive (false);
        Score = Score + 1;
        SetScoreText ();
    }
}

void SetScoreText ()
{
    ScoreText.text = "Score: " + Score.ToString ();
} 

这是我的generateEnemy脚本:

public GameOverManager gameOverManager = null;

[HideInInspector]
public float minBlobSpawnTime = 2;
[HideInInspector]
public float maxBlobSpawnTime = 5;
[HideInInspector]
public bool generateBlobs = true;
[HideInInspector]
public GameObject blobPrefab = null;
[HideInInspector]
public GameObject blobRoot = null;
[HideInInspector]
public float minimumYPosition = -0.425f;
[HideInInspector]
public float maximumYPosition = 0.35f;

[HideInInspector]
public float minDaggerSpawnTime = 2;
[HideInInspector]
public float maxDaggerSpawnTime = 5;
[HideInInspector]
public bool generateDaggers = true;
[HideInInspector]
public GameObject daggerPrefab = null;
[HideInInspector]
public GameObject daggerRoot;
[HideInInspector]
public float minimumXPosition = -11.5f;
[HideInInspector]
public float maximumXPosition = 11.5f;

public Camera camera = null;

// Use this for initialization
void Start ()
{
    generateBlobs = ((generateBlobs) && (blobPrefab != null) && (blobRoot != null));
    generateDaggers = ((generateDaggers) && (daggerPrefab != null) && (daggerRoot != null));

    if (camera == null)
    {
        Debug.LogError("GenerateEnemy: camera is not set in the inspector. Please set and try again.");
        camera = Camera.main;
    }

    if (gameOverManager == null)
    {
        Debug.LogError("GenerateEnemy: gameOverManager not set in the inspector. Please set and try again.");
    }

    if (generateBlobs)
    {
        StartCoroutine(GenerateRandomEnemy(true, blobPrefab, blobRoot, minBlobSpawnTime, maxBlobSpawnTime));
    }

    if (generateDaggers)
    {
        StartCoroutine(GenerateRandomEnemy(false, daggerPrefab, daggerRoot, minDaggerSpawnTime, maxDaggerSpawnTime));
    }
}

// Update is called once per frame
void Update ()
{
    DestroyOffScreenEnemies();
}

// Spawn an enemy
IEnumerator GenerateRandomEnemy(bool generateOnYAxis, GameObject prefab, GameObject root, float minSpawnTime, float maxSpawnTime)
{
    if ((prefab != null) && (gameOverManager != null))
    {
        if (!gameOverManager.GameIsPaused())
        {
            GameObject newEnemy = (GameObject) Instantiate(prefab);
            newEnemy.transform.SetParent(root.transform, true);

            // set this in the prefab instead
            // newEnemy.transform.position = new Vector3(newEnemy.transform.parent.position.x, 0.5f, newEnemy.transform.parent.position.z);

            // or if you want the y position to be random you need to do something like this
            if (generateOnYAxis)
            {
                newEnemy.transform.position = new Vector3(newEnemy.transform.parent.position.x, Random.Range(minimumYPosition, maximumYPosition), newEnemy.transform.parent.position.z);
            }
            else
            {
                newEnemy.transform.position = new Vector3(Random.Range(minimumXPosition, maximumXPosition), newEnemy.transform.parent.position.y, newEnemy.transform.parent.position.z);
            }
        }
    }
    yield return new WaitForSeconds(Random.Range(minSpawnTime, maxSpawnTime));

    StartCoroutine(GenerateRandomEnemy(generateOnYAxis, prefab, root, minSpawnTime, maxSpawnTime));
}

public void DestroyOffScreenEnemies()
{
    GameObject[] enemies = GameObject.FindGameObjectsWithTag("enemy");
    if ((enemies.Length > 0) && (camera != null))
    {
        for  (int i = (enemies.Length - 1); i >= 0; i--)
        {
            // just a precaution
            if ((enemies[i] != null) && ((camera.WorldToViewportPoint(enemies[i].transform.position).x > 1.03f) || 
                                         (enemies[i].transform.position.y < -6f)))
            {
                Destroy(enemies[i]);
            }
        }
    }
}

}

(此脚本有一个引用它的generateEnemyCustomEditor脚本,在检查器中显示)

谢谢:)

1 个答案:

答案 0 :(得分:1)

首先,从static中删除public static int movespeed = 20;关键字,如果要从其他脚本修改GameObject.Find("ObjectMovementIsAttachedTo").GetComponent<Movement>();变量,请使用movespeed获取脚本实例。

  

当玩家收集另外10分等等时更快   上?

解决方案是直接的。使用

if (Score % 10 == 0){
  //Increement by number (4) movespeed from Movement script
    movement.movespeed += 4;
} 

检查Score是否增加10,然后将movespeed增加所需的任何值,如果该条件为true。在得分增加OnTriggerEnter2D后将其放入1函数中是有意义的。

您的新score脚本:

public class score : MonoBehaviour
{
    public int Score;
    public Text ScoreText;

    private int moveSpeed;

    void Start()
    {
        Score = 0;
        SetScoreText();
    }

    void OnTriggerEnter2D(Collider2D other)
    {
        if (other.gameObject.CompareTag("Pick Up"))
        {
            other.gameObject.SetActive(false);
            Score = Score + 1;
            if (Score % 10 == 0)
            {
                //Increement by number (4) movespeed from Movement script
                moveSpeed += 4;
            }
            SetScoreText();
        }
    }

    public int getMoveSpeed()
    {
        return moveSpeed;
    }

    void SetScoreText()
    {
        ScoreText.text = "Score: " + Score.ToString();
    }
}

由于您的Movement脚本将被实例化,因此当您实例化它时,您将其引用发送到Player脚本。

public class Movement : MonoBehaviour
{
    public int movespeed = 20;
    public Vector3 userDirection = Vector3.right;

    score mySpeed;

    void Start()
    {
        //Send Movement instance to the score script
        GameObject scoreGameObject = GameObject.Find("GameObjectScoreIsAttachedTo");
        mySpeed = scoreGameObject.GetComponent<score>();
    }

    public void Update()
    {
        transform.Translate(userDirection * mySpeed.getMoveSpeed() * Time.deltaTime);
    }
}