Unity2D: How to make spawn object gradually go faster after the player collects 10 points?

£可爱£侵袭症+ 提交于 2019-12-12 04:23:46

问题


so I was wondering if there was a way to make a spawned object gradually moves/go faster after the player (you the user) collects 10 points. And faster when the player collects another 10 points and so on and so on?

This is my movement script attach to my objects that get spawned in:

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

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

This is my score script attach to my player

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 ();
} 

And this is my generateEnemy script:

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]);
            }
        }
    }
}

}

(This script has an generateEnemyCustomEditor script that references it, to show in the inspector)

Thank you :)


回答1:


First of all, remove the static keyword from public static int movespeed = 20; and use GameObject.Find("ObjectMovementIsAttachedTo").GetComponent<Movement>(); to get the script instance if you want to modify movespeed variable from another script.

And faster when the player collects another 10 points and so on and so on?

The solution is straight on. Use

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

to check if the Score is increased by 10 then increment movespeed by any value you want if that condition is true. It makes sense to put that in the OnTriggerEnter2D function after Score is incremented by 1.

Your new score script:

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();
    }
}

Since your Movement script will be instantiated, when you instantiate it, you send its reference to the Player script.

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);
    }
}


来源:https://stackoverflow.com/questions/38907515/unity2d-how-to-make-spawn-object-gradually-go-faster-after-the-player-collects

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!