如何更改Gizmos.DrawWireCube
的宽度,基于子变换(Gizmos.DrawWireSphere
)transform.position
或者变换的左/右极限?
到目前为止我的代码:
position.cs
using UnityEngine;
using System.Collections;
public class Position : MonoBehaviour {
void OnDrawGizmos () {
Gizmos.DrawWireSphere(transform.position, 0.75f);
}
}
FormationController.cs
using UnityEngine;
using System.Collections;
public class FormationController : MonoBehaviour {
public GameObject enemyPrefab;
public float width = 10;
public float height = 5;
public float speed = 5;
public float padding = 1.5f;
public float SpawnDelaySeconds = 1;
private int direction = 1;
private float boundaryLeftEdge,boundaryRightEdge;
private PlayerController playerController;
// Use this for initialization
void Start () {
float distance = transform.position.z - Camera.main.transform.position.z;
Vector3 leftmost = Camera.main.ViewportToWorldPoint(new Vector3(0, 0, distance));
Vector3 rightmost = Camera.main.ViewportToWorldPoint(new Vector3(1, 0, distance));
boundaryLeftEdge = leftmost.x + padding;
boundaryRightEdge = rightmost.x - padding;
SpawnUntilFull();
}
void OnDrawGizmos () {
Gizmos.DrawWireCube(transform.position, new Vector3 (width, height));
}
// Update is called once per frame
void Update () {
float formationRightEdge = transform.position.x + 0.5f * width;
float formationLeftEdge = transform.position.x - 0.5f * width;
if (formationRightEdge > boundaryRightEdge) {
direction = -1;
}
if (formationLeftEdge < boundaryLeftEdge) {
direction = 1;
}
transform.position += new Vector3(direction * speed * Time.deltaTime, 0, 0);
if (AllEnemiesAreDead()) {
SpawnUntilFull();
}
}
void SpawnUntilFull () {
Transform freePos = NextFreePosition();
GameObject enemy = Instantiate(enemyPrefab, freePos.position, Quaternion.identity) as GameObject;
enemy.transform.parent = freePos;
if (FreePositionExists()) {
Invoke ("SpawnUntilFull", SpawnDelaySeconds);
}
}
bool FreePositionExists () {
foreach( Transform position in transform) {
if (position.childCount <= 0) {
return true;
}
}
return false;
}
Transform NextFreePosition() {
foreach (Transform position in transform) {
if (position.childCount <= 0) {
return position;
}
}
return null;
}
bool AllEnemiesAreDead () {
foreach(Transform position in transform) {
if (position.childCount > 0) {
return false;
}
}
return true;
}
}
示例:
我正在做一个像“太空入侵者”这样的游戏,但是我无法弄清楚当最左边的入侵者死亡时,入侵者阵型会变成更小的阵型...所以新的最左边的入侵者“接触”了左边界。
我的场景:
忽略最顶部的小发明(圆圈)
答案 0 :(得分:1)
试试这个:
using UnityEngine;
using System.Collections;
public class FormationController : MonoBehaviour {
public GameObject enemyPrefab;
public float widthToLeft = 5;
public float widthToRight = 5;
public float height = 5;
public float speed = 5;
public float padding = 1.5f;
public float SpawnDelaySeconds = 1;
private int direction = 1;
private float boundaryLeftEdge,boundaryRightEdge;
private PlayerController playerController;
// Use this for initialization
void Start () {
float distance = transform.position.z - Camera.main.transform.position.z;
Vector3 leftmost = Camera.main.ViewportToWorldPoint(new Vector3(0, 0, distance));
Vector3 rightmost = Camera.main.ViewportToWorldPoint(new Vector3(1, 0, distance));
boundaryLeftEdge = leftmost.x + padding;
boundaryRightEdge = rightmost.x - padding;
SpawnUntilFull();
}
void OnDrawGizmos () {
Gizmos.DrawWireCube(transform.position, new Vector3 (widthToLeft+widthToRight, height));
}
// Update is called once per frame
void Update () {
float formationRightEdge = transform.position.x + 0.5f * widthToRight;
float formationLeftEdge = transform.position.x - 0.5f * widthToLeft;
if (formationRightEdge > boundaryRightEdge) {
direction = -1;
}
if (formationLeftEdge < boundaryLeftEdge) {
direction = 1;
}
transform.position += new Vector3(direction * speed * Time.deltaTime, 0, 0);
if (AllEnemiesAreDead()) {
SpawnUntilFull();
}
}
void SpawnUntilFull () {
Transform freePos = NextFreePosition();
GameObject enemy = Instantiate(enemyPrefab, freePos.position, Quaternion.identity) as GameObject;
enemy.transform.parent = freePos;
if (FreePositionExists()) {
Invoke ("SpawnUntilFull", SpawnDelaySeconds);
}
}
bool FreePositionExists () {
foreach( Transform position in transform) {
if (position.childCount <= 0) {
return true;
}
}
return false;
}
Transform NextFreePosition() {
foreach (Transform position in transform) {
if (position.childCount <= 0) {
return position;
}
}
return null;
}
bool AllEnemiesAreDead () {
foreach(Transform position in transform) {
if (position.childCount > 0) {
return false;
}
}
return true;
}
}
我刚刚将width更改为widthToLeft和widthToRight
当入侵者死亡时:
public void OnInvaderDeath() {
float mostLeft = 10000;
float mostRight = -10000;
foreach( Transform invaderTransform in invaderList ) {
if( invaderTransform.position.x < mostLeft )
mostLeft = invaderTransform.position.x;
if( invaderTransform.position.x > mostRight )
mostRight = invaderTransform.position.x;
}
widthToLeft = mostLeft + 0.75f;
widthToRight = mostRight + 0.75f;
}