Nostalgia (Version 1.0.2) では前回示したようなコライダーごとにスクリプトを付ける手法は使えないようなので、プレイヤーの近傍のセルを調べてcollider.enabled を切り替える方法にした。
図で示すとこういう感じ。画像の赤色のセルはプレイヤーのいるセル、緑色のセルは、中にコライダーがあれば collider.enabled = false にするセル、青色のセルは、中にコライダーがあれば collider.enabled = true にするセルを示す。
KS コードを公開するよ。コメントをつけたやたらに長い名前の3つの変数の値をお好みで適当にいじればいいんじゃないかな。
using UnityEngine;
using System.Collections;
using Nostalgia;
public class NostalgiaColliderEnabledSwithcher : MonoBehaviour
{
Transform playerTransform;
Map map;
Vector3 playerPosition;
int numberOfHalfOfHorizontalCells;
// 中にコライダーがあれば collider.enabled の値を変更するセル (図の青色または緑色のセル) の水平方向の数。
int numberOfHorizontalCells = 7;
// 中にコライダーがあれば collider.enabled = false にするセル (図の緑色のセル) の垂直方向の数。
int numberOfVerticalCellsEachOfWhichMayHaveColliderToBeDisabled = 6;
// 中にコライダーがあれば collider.enabled = true にするセル (図の青色のセル) の垂直方向の数。
int numberOfVerticalCellsEachOfWhichMayHaveColliderToBeEnabled = 1;
Point2[] offset1;
Point2[] offset2;
void Start ()
{
playerTransform = GameObject.FindGameObjectWithTag ("Player").transform;
map = GetComponent <Map> ();
numberOfHalfOfHorizontalCells = (numberOfHorizontalCells - 1) / 2;
offset1 = new Point2[numberOfHorizontalCells * numberOfVerticalCellsEachOfWhichMayHaveColliderToBeDisabled];
for (int i = 0; i < numberOfHorizontalCells; i++) {
for (int j = 0; j < numberOfVerticalCellsEachOfWhichMayHaveColliderToBeDisabled; j++) {
offset1 [numberOfVerticalCellsEachOfWhichMayHaveColliderToBeDisabled * i + j] = new Point2 (i - numberOfHalfOfHorizontalCells, j);
}
}
offset2 = new Point2[numberOfHorizontalCells * numberOfVerticalCellsEachOfWhichMayHaveColliderToBeEnabled];
for (int i = 0; i < numberOfHorizontalCells; i++) {
for (int j = 0; j < numberOfVerticalCellsEachOfWhichMayHaveColliderToBeEnabled; j++) {
offset2 [numberOfVerticalCellsEachOfWhichMayHaveColliderToBeEnabled * i + j] = new Point2 (i - numberOfHalfOfHorizontalCells, - 1 - j);
}
}
}
void FixedUpdate ()
{
playerPosition = playerTransform.position;
Point2 mapPointOfPlayerPosition = map.WorldPointToMapPoint (playerPosition);
Point2 [] listOfPoint2InCellWhichMayHaveColliderToBeDisabled = new Point2[numberOfHorizontalCells * numberOfVerticalCellsEachOfWhichMayHaveColliderToBeDisabled];
for (int i = 0; i < numberOfHorizontalCells; i++) {
for (int j = 0; j < numberOfVerticalCellsEachOfWhichMayHaveColliderToBeDisabled; j++) {
listOfPoint2InCellWhichMayHaveColliderToBeDisabled [numberOfVerticalCellsEachOfWhichMayHaveColliderToBeDisabled * i + j] = mapPointOfPlayerPosition + offset1 [numberOfVerticalCellsEachOfWhichMayHaveColliderToBeDisabled * i + j];
}
}
foreach (Point2 p in listOfPoint2InCellWhichMayHaveColliderToBeDisabled) {
SwitchColliderEnabled (p, false);
}
Point2[] listOfPoint2InCellWhichMayHaveColliderToBeEnabled = new Point2[numberOfHorizontalCells * numberOfVerticalCellsEachOfWhichMayHaveColliderToBeEnabled];
for (int i = 0; i < numberOfHorizontalCells; i++) {
for (int j = 0; j < numberOfVerticalCellsEachOfWhichMayHaveColliderToBeEnabled; j++) {
listOfPoint2InCellWhichMayHaveColliderToBeEnabled [numberOfVerticalCellsEachOfWhichMayHaveColliderToBeEnabled * i + j] = mapPointOfPlayerPosition + offset2 [numberOfVerticalCellsEachOfWhichMayHaveColliderToBeEnabled * i + j];
}
}
foreach (Point2 p in listOfPoint2InCellWhichMayHaveColliderToBeEnabled) {
SwitchColliderEnabled (p, true);
}
}
void SwitchColliderEnabled (Point2 givenPoint2, bool b)
{
Cell cellOfGivenPoint2 = map.GetCell (givenPoint2);
if (cellOfGivenPoint2 != null) {
Collider2D colliderOfCell = cellOfGivenPoint2.collider;
if (colliderOfCell != null) {
colliderOfCell.enabled = b;
}
}
}
}
使用した Nostalgia の Version 1.0.2 を追記した。 2014-09-25
0 件のコメント:
コメントを投稿