0

私はオンラインのチュートリアルに従っていましたが、基本的に、スクリプトは、カメラが特定の位置に到達した場合にゲームオブジェクト自体のクローンをインスタンス化するように指示しています。私が混乱している最後の行はこれらです

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

1 に答える 1