Unity - A计划(永久有效期) 扫二维码继续学习 二维码时效为半小时

(197评价)
价格: 4431.00元
关于涂鸦弹跳的startPosition问题
helloWHH发起了问答2019-08-02
1
回复
225
浏览

大家好,我这边涂鸦弹跳的startPosition得到的结果都是(-3.9, -1.1, 4.4),但tile的具体生成的位置分别是(6.6, 13.5, 0.0),(7.4, 10.5, 0.0)等等不一样位置,所以导致tile会移动到固定的x或者y值上后再左右或者上下移动,不知道为什么transform.position得到的值会是(-3.9, -1.1, 4.4)固定不变的?这边在gameManager 里边tile生成时的位置是正确的没有问题,但不知道是什么原因导致无法在Tile类里边无法获取正确的位置,最终导致出现下图一样的情况:

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

public class GameManager : MonoSingleton<GameManager> {
    private Queue<GameObject>tilePool=new Queue<GameObject>();
    private Queue<GameObject> itemPool = new Queue<GameObject>();
    private Queue<GameObject> enemyPool = new Queue<GameObject>();
    private Queue<GameObject> coinPool = new Queue<GameObject>();
    private Queue<GameObject> bulletPool = new Queue<GameObject>();
    private GameObject tilePrefab;
    private int initialSize = 60;
    //总的生成比例
    private float totalSum;
    public GameSetting gameSetting;
    private float currentY=-7.5f;
    public GameObject Floor;
    public Vector2 startPosition;
    private float distance;
    private float speed;
    private int moveDirection;//0 left 1 right//0 up 1 down
    public Tile tile;
    protected override void Awake()
    {
        base.Awake();
        tilePrefab = (GameObject)Resources.Load("Prefabs/Tile");
    }

    void Start()
    {
        //赋权重
        GetAllWeights();
        //初始化对象池
        GenerateTilePool();
        //生成物体
        for (int i = 0; i < initialSize; i++)
        {
            GenerateRandomTile();
        }
        
    }
    #region 生成tile池
    private void GenerateTilePool()
    {
        for (int i = 0; i < initialSize; i++)
        {
            GameObject go = Instantiate(tilePrefab, transform);
            go.SetActive(false);
            go.name = i.ToString();
            tilePool.Enqueue(go);
        }
    }
    #endregion
    #region 生成随机砖块
    void GenerateRandomTile()
    {
        GameObject tile = GetInActiveObject(ObjectType.Tile);
        float random = Random.Range(0, totalSum);
        int RandomNumber = SetTileByRandomNumber(random);
        Vector2 vector2=new Vector2(Random.Range(-0.35f,8.39f),currentY);
        switch (RandomNumber)
        {
            //NromalTile 0
            //BrkenTile 1
            //OneTimeOnly 2
            //SpringTile 3
            //MovingHorizontally 4
            //MovingVertically 5
            case 0:
                tile.name = "0";
                tile.SetActive(true);
                tile.transform.localPosition = vector2;
                currentY += Random.Range(gameSetting.normaltile.minHight+2, gameSetting.normaltile.maxHight);
                break;
            case 1:
                tile.name = "1";
                tile.SetActive(true);
                tile.transform.localPosition = vector2;
                currentY += Random.Range(gameSetting.brokentile.minHight + 2, gameSetting.brokentile.maxHight);
                break;
            case 2:
                tile.name = "2";
                tile.SetActive(true);
                tile.transform.localPosition = vector2;
                currentY += Random.Range(gameSetting.onetimeonly.minHight + 2, gameSetting.onetimeonly.maxHight);
                break;
            case 3:
                tile.name = "3";
                tile.SetActive(true);
                tile.transform.localPosition = vector2;
                currentY += Random.Range(gameSetting.springtile.minHight + 2, gameSetting.springtile.maxHight);
                break;
            case 4:
                tile.name = "4";
                tile.SetActive(true);
                tile.transform.localPosition = vector2;
                currentY += Random.Range(gameSetting.movinghorizontally.minHight + 2, gameSetting.movinghorizontally.maxHight);
                Debug.Log("GameManager 4 transform.position:"+tile.transform.localPosition);
                break;
            case 5:
                tile.name = "5";
                tile.SetActive(true);
                tile.transform.localPosition = vector2;
                currentY += Random.Range(gameSetting.movingvertically.minHight + 2, gameSetting.movingvertically.maxHight);
                Debug.Log("GameManager 5 transform.position:" + tile.transform.localPosition);
                break;
            case -1:
                break;
        }
    }
    #endregion
    #region 获取非活对象
    GameObject GetInActiveObject(ObjectType type)
    {
        switch (type)
        {
            case ObjectType.Tile:
                return tilePool.Dequeue();
            case ObjectType.Item:
                return itemPool.Dequeue();
            case ObjectType.Enemy:
                return enemyPool.Dequeue();
            case ObjectType.Bullet:
                return bulletPool.Dequeue();
            case ObjectType.Coin:
                return coinPool.Dequeue();
            default:
                return null;
        }
    }
    #endregion
    #region 获取所有砖块的比例
    void GetAllWeights()
    {
        float sum = 0;
        sum += gameSetting.normaltile.weight;
        sum += gameSetting.brokentile.weight;
        sum += gameSetting.springtile.weight;
        sum += gameSetting.onetimeonly.weight;
        sum += gameSetting.movinghorizontally.weight;
        sum += gameSetting.movingvertically.weight;
        totalSum = sum;
    }
    #endregion
    #region 随机生成数字
    //NromalTile 0
    //BrkenTile 1
    //OneTimeOnly 2
    //SpringTile 3
    //MovingHorizontally 4
    //MovingVertically 5
    int SetTileByRandomNumber(float number)
    {
        if (number<=gameSetting.normaltile.weight)
        {
            return 0;
        }
        else if (number<= gameSetting.normaltile.weight+gameSetting.brokentile.weight)
        {
            return 1;
        }
        else if (number <= gameSetting.normaltile.weight + gameSetting.brokentile.weight + 
                 gameSetting.onetimeonly.weight)
        {
            return 2;
        }
        else if (number <= gameSetting.normaltile.weight + gameSetting.brokentile.weight + 
                 gameSetting.onetimeonly.weight+gameSetting.springtile.weight)
        {
            return 3;
        }
        else if (number <= gameSetting.normaltile.weight + gameSetting.brokentile.weight + 
                 gameSetting.onetimeonly.weight + gameSetting.springtile.weight+gameSetting.movinghorizontally.weight)
        {
            return 4;
        }
        else if (number <= gameSetting.normaltile.weight + gameSetting.brokentile.weight + 
                 gameSetting.onetimeonly.weight + gameSetting.springtile.weight + gameSetting.movinghorizontally.weight+gameSetting.movingvertically.weight)
        {
            return 5;
        }
        return -1;
    }


    #endregion

    #region 将非活的游戏物体移回tile池

    public void AddInactiveObjectToPool(GameObject gameObject,ObjectType type)
    {
        //先隐藏,再回收
        gameObject.SetActive(false);
        switch (type)
        {
            case  ObjectType.Tile:
                tilePool.Enqueue(gameObject);
                //在生成新的砖块

                break;
            case  ObjectType.Item:
                itemPool.Enqueue(gameObject);
                break;
            case ObjectType.Enemy:
                enemyPool.Enqueue(gameObject);
                break;
            case ObjectType.Bullet:
                bulletPool.Enqueue(gameObject);
                break;
            case ObjectType.Coin:
                coinPool.Enqueue(gameObject);
                break;
        }
        
    }
    #endregion

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

public class Tile : MonoBehaviour
{
    public int TileType;
    public SpriteRenderer SpriteRenderer;
    private ManagerVars Vars;
    private float speed;
    private float distance;
    public Vector3 startPosition;
    private int moveDirection;//0 left 1 right//0 up 1 down

    void OnEnable()
    {
        Init();
    }
    private void Init()
    {
        SpriteRenderer = GetComponent<SpriteRenderer>();
        Vars = ManagerVars.GetManagerVars();
        Debug.Log(gameObject.name);
        string name = gameObject.name;
        int tileValue;
        int.TryParse(name, out tileValue);
        TileType = tileValue;
        startPosition = gameObject.transform.position;
        switch (TileType)
        {
            //NromalTile 0
            //BrkenTile 1
            //OneTimeOnly 2
            //SpringTile 3
            //MovingHorizontally 4
            //MovingVertically 5
            case 0:
                SpriteRenderer.sprite = Vars.Sprites[0];
                break;
            case 1:
                SpriteRenderer.sprite = Vars.Sprites[1];
                break;
            case 2:
                SpriteRenderer.sprite = Vars.Sprites[2];
                break;
            case 3:
                SpriteRenderer.sprite = Vars.Sprites[3];
                break;
            case 4:
                //MovingHorizontally 4
                SpriteRenderer.sprite = Vars.Sprites[4];
                speed = GameManager.Instance.gameSetting.movinghorizontally.speed;
                distance = GameManager.Instance.gameSetting.movinghorizontally.distance;
                startPosition = gameObject.transform.position;
                Debug.Log("4 startPosition:"+ startPosition);
                break;
            case 5:
                //MovingVertically 5
                SpriteRenderer.sprite = Vars.Sprites[5];
                speed = GameManager.Instance.gameSetting.movingvertically.speed;
                distance = GameManager.Instance.gameSetting.movingvertically.distance;
                startPosition = gameObject.transform.position;
                Debug.Log("5 startPosition:" + startPosition);
                break;
        }
    }

    void OnTriggerEnter2D(Collider2D collider2D)
    {
        //当碰撞的对像的tag是player以及碰撞的对象处于下落的情况
        if (collider2D.tag=="Player"&&collider2D.GetComponent<Rigidbody2D>().velocity.y<=0)
        {
            switch (TileType)
            {
                //NromalTile 0
                //BrkenTile 1
                //OneTimeOnly 2
                //SpringTile 3
                //MovingHorizontally 4
                //MovingVertically 5
                case 0:
                    collider2D.GetComponent<PlayerController>().Jump(1);
                    break;
                case 1:
                    GetComponent<Rigidbody2D>().gravityScale = 1;
                    break;
                case 2:
                    collider2D.GetComponent<PlayerController>().Jump(1);
                    GetComponent<Rigidbody2D>().gravityScale = 1;
                    break;
                case 3:
                    collider2D.GetComponent<PlayerController>().Jump(2);
                    break;
                case 4:
                    collider2D.GetComponent<PlayerController>().Jump(1);
                    break;
                case 5:
                    collider2D.GetComponent<PlayerController>().Jump(1);
                    break;
                default:
                    break;
            }
        }
    }
	void Update () {
        #region 砖块移动功能
        switch (TileType)
        {
            case 4:
                //MovingHorizontally 4

                if (moveDirection==0)//往左边移动
                {
                    transform.Translate(new Vector3(-speed*Time.deltaTime,0,0));
                    if (startPosition.x - transform.position.x > distance)
                    {
                        moveDirection = 1;//转方向移动
                    }
                }
                else//向右移动
                {
                    transform.Translate(new Vector3(speed * Time.deltaTime, 0,0));
                    if (startPosition.x - transform.position.x < -distance)
                    {
                        moveDirection = 0;//转方向移动
                    }
                }
                break;
            case 5:
                //MovingVertically
                if (moveDirection == 0)
                {
                    transform.Translate(new Vector3(0,speed*Time.deltaTime,0));
                    if (startPosition.y-transform.position.y<-distance)
                    {
                        moveDirection = 1;
                    }
                }
                else
                {
                    transform.Translate(new Vector3(0,-speed*Time.deltaTime,0));
                    if (startPosition.y-transform.position.y>distance)
                    {
                        moveDirection = 0;
                    }
                }
                break;
        }
        #endregion

        if (GameManager.Instance.Floor.transform.position.y>transform.position.y+1)
        {
            GetComponent<Rigidbody2D>().gravityScale = 0;
            GameManager.Instance.AddInactiveObjectToPool(transform.gameObject,ObjectType.Tile);
        }
	}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[CreateAssetMenu(menuName = "CreateManagerAssetContainer")]
public class ManagerVars : ScriptableObject{
    public static ManagerVars GetManagerVars()
    {
        return Resources.Load<ManagerVars>("ManagerVarsContainer");
    }
    public Sprite[] Sprites;
}

 

所有回复
发表回复
你还没有登录,请先 登录或 注册!