在Unity中为2D平台平铺

时间:2014-06-01 21:02:12

标签: unity3d tiling

我在网上关注了一个教程,基本上脚本告诉gameObject如果摄像机到达某个位置就会实例化自身的克隆。我困惑的最后几行是这些

  if (rightOrLeft > 0) 
    {
    newBuddy.GetComponent<Tiling>().hasALeftBuddy = true;
    }
else 
    {
    newBuddy.GetComponent<Tiling>().hasARightBuddy = true;
    }
}

简而言之,我在这里并没有真正与语法或数学相提并论。有人可以帮我帮忙清理一下吗?

using UnityEngine;
using System.Collections;

[RequireComponent (typeof(SpriteRenderer))]

public class Tiling : MonoBehaviour {

public int offsetX = 2;         

public bool hasARightBuddy = false;
public bool hasALeftBuddy = false;

public bool reverseScale = false;   


private float spriteWidth = 0f;     

private Camera cam;
private Transform myTransform;

void Awake () {
    cam = Camera.main;
    myTransform = transform;
}

// Use this for initialization
void Start () {
    SpriteRenderer sRenderer = GetComponent<SpriteRenderer>();
    spriteWidth = sRenderer.sprite.bounds.size.x;
}

// Update is called once per frame
void Update ()
{


    if (hasALeftBuddy == false || hasARightBuddy == false) 
    {

        float camHorizontalExtend = cam.orthographicSize * Screen.width/Screen.height;


        float edgeVisiblePositionRight = (myTransform.position.x + spriteWidth/2) - camHorizontalExtend; //sprite width/2..51.2  
                                                                                                         //(0 + 51.2 - 26.67315 = 24.52685)
                                                                                                         //(102.4 + 51.2 - 26.67315 = 126.92685) etc.. //clone of the myTransform
        float edgeVisiblePositionLeft = (myTransform.position.x - spriteWidth/2) + camHorizontalExtend; // (0 - 51.2 + 26.67315 = -24.52685)
                                                                                                        // (-102.4 - 51.2 + 26.67315 = -126.92685) etc..//clone of the myTransform

        if (cam.transform.position.x >= edgeVisiblePositionRight - offsetX && hasARightBuddy == false)
        {
            MakeNewBuddy (1);
            hasARightBuddy = true;
        }
        else if (cam.transform.position.x <= edgeVisiblePositionLeft + offsetX && hasALeftBuddy == false)
        {
            MakeNewBuddy (-1);
            hasALeftBuddy = true;
        }
    }
}


void MakeNewBuddy (int rightOrLeft)
{

    Vector3 newPosition = new Vector3 (myTransform.position.x + spriteWidth * rightOrLeft, myTransform.position.y, myTransform.position.z);

    Transform newBuddy = Instantiate (myTransform, newPosition, myTransform.rotation) as Transform;


    if (reverseScale == true) 
    {
        newBuddy.localScale = new Vector3 (newBuddy.localScale.x*-1, newBuddy.localScale.y, newBuddy.localScale.z);
    }


    if (rightOrLeft > 0) 
        {
        newBuddy.GetComponent<Tiling>().hasALeftBuddy = true;
        }
    else 
        {
        newBuddy.GetComponent<Tiling>().hasARightBuddy = true;
        }
    }
}
}

1 个答案:

答案 0 :(得分:1)

在我看来,代码告诉当前游戏对象在创建新邻居时它有一个邻居。

布尔值有一个邻居变量很方便,可以确定这个创建过程不需要再次发生:)