简体   繁体   中英

Change direction of obstacles script unity

I am trying to change the directions of the obstacles in this script from going across the screen horizontally to vertically. Here is the script:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

    public class ObSpawn : MonoBehaviour
    {
        public GameObject asteroidPrefab;
        public float respawnTime = 1.0f;
        private Vector2 screenBounds;
    
        // Use this for initialization
        void Start()
        {
            screenBounds = Camera.main.ViewportToWorldPoint(
            new Vector3(0f, 0f, -Camera.main.transform.position.z));
            StartCoroutine(asteroidWave());
        }
        private void spawnEnemy()
        {
            GameObject a = Instantiate(asteroidPrefab) as GameObject;
            a.transform.position = new Vector2(screenBounds.y * -2, Random.Range(-screenBounds.y * -1, screenBounds.y * -1));
        }
        IEnumerator asteroidWave()
        {
            while (true)
            {
                yield return new WaitForSeconds(respawnTime);
                spawnEnemy();
            }
        }
    }

I do not know how to change the direction. Here is the website I got the script from; the code is a bit different beacause it was not initially working the way I wanted it to. https://pressstart.vip/tutorials/2018/09/25/58/spawning-obstacles.html Hope you can help

As mentioned in comments, the code in the question covers spawning but not movement. Here's an attempt at an answer to your intended question based off the code from your link. Be aware that the code you're working from requires the camera to stay fixed at x=0, y=0.

In SpawnEnemy() , change:

    a.transform.position = new Vector2(screenBounds.y * -2, Random.Range(-screenBounds.y * -1, screenBounds.y * -1));

to:

    a.transform.position = new Vector2( Random.Range(screenBounds.x, -screenBounds.x), screenBounds.y * -2 );

And then assuming your code is the same as the code at your tutorial link , in Start() on the asteroids change:

    rb.velocity = new Vector2(-speed, 0);
    screenBounds = Camera.main.ScreenToWorldPoint(new Vector3(Screen.width, Screen.height, Camera.main.transform.position.z));

to:

    rb.velocity = new Vector2(0, -speed);
    screenBounds = Camera.main.ScreenToWorldPoint(new Vector3(0, 0, -Camera.main.transform.position.z));

(the screenBounds change is for consistency with your other code).

In Update () on the asteroids change:

    if(transform.position.x < screenBounds.x * 2){
        Destroy(this.gameObject);
    }

to:

    if(transform.position.y < screenBounds.y){
        Destroy(this.gameObject);
    }

If you want to have both vertically and horizontally moving obstacles simultaneously that's a different problem.

Assuming the tutorial video explains the concepts used, I'd really suggest re-watching it and trying to understand what's actually being done and why. I'm kind of regretting having typed this out at the moment as it's avoiding the root issue, but hopefully being able to compare the various versions will be of some use to you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM