Using UnityEngine;
using System;
using System.Collections;
using System.Collections.Generic;
public class GameController : MonoBehaviour {
private CubePos nowCube = new CubePos ( 0, 1, 0 );
private float cubeChangePlaceSpeed = 0.5f;
public Transform cubeToPlace;
private List allCubesPositions = new List{
new Vector3(0,0,0),
new Vector3(1,0,0),
new Vector3(-1,0,0),
new Vector3(0,1,0),
new Vector3(0,0,1),
new Vector3(0,0,-1),
new Vector3(1,0,1),
new Vector3(-1,0,-1),
new Vector3(-1,0,1),
new Vector3(1,0,-1),
};
private void Start (){
StartCoroutine(ShowCubePlace());
}
IEnumerator ShowCubePlace() {
while(true) {
SpawnPositions();
yield return new WaitForSecond(cubeChangePlaceSpeed);
}
}
private void SpawnPositions(){
List positions = new List();
if(IsPositionEmpty(new Vector3 (nowCube.x + 1,nowCube.y,nowCube.z)))
& nowCube.x + 1 != cubeToPlace.position.x
positions.Add(new Vector3 (nowCube.x + 1,nowCube.y,nowCube.z));
if ((IsPositionEmpty(new Vector3 (nowCube.x - 1,nowCube.y,nowCube.z)))
& nowCube.x - 1 != cubeToPlace.position.x
positions.Add(new Vector3 pos(nowCube.x - 1,nowCube.y,nowCube.z));
if ((IsPositionEmpty(new Vector3 (nowCube.x,nowCube.y + 1,nowCube.z)));
& nowCube.y + 1 != cubeToPlace.position.y
positions.Add(new Vector3 (nowCub.x,nowCub.y + 1,nowCub.z));
if ((IsPositionEmpty(new Vector3 (nowCube.x,nowCube.y - 1,nowCube.z)))
& nowCube.y - 1 != cubeToPlace.position.y;
positions.Add(new Vector3 (nowCub.x,nowCub.y - 1,nowCub.z));
if ((IsPositionEmpty(new Vector3 (nowCube.x,nowCube.y,nowCube.z + 1)));
& nowCube.z + 1 != cubeToPlace.position.z
positions.Add(new Vector3 (nowCub.x,nowCub.y,nowCub.z + 1));
if ((IsPositionEmpty(new Vector3 (nowCube.x,nowCube.y,nowCube.z - 1)))
& nowCube.z - 1 != cubeToPlace.position.z;
positions.Add(new Vector3 (nowCub.x,nowCub.y,nowCub.z - 1));
cubeToPlace.position = positions [UnityEngine.Random.Range(0,positions.Count)];
Debug.Log(positions.Count);
}
private bool isPositionEmpty(Vector3 targetPos){
if(targetPos.y == 0)
return false ;
foreach(Vector3 pos in allCubesPositions ){
if(pos.x == targetPos.x & pos.y == targetPos.y & pos.z == targetPos.z)
return false;
}
return true;
}
struct CubePos {
public int x,y,z;
public CubePos(int x,int y,int z) {
this.x = x;
this.y = y;
this.z = z;
}
public Vector3 getVector(){
return new Vector3(x,y,z);
}
public void setVector(Vector3 pos) {
x = Convert.ToInt32(pos.x);
y = Convert.ToInt32(pos.y);
z = Convert.ToInt32(pos.z);
}
}
C#
Пожалуйста помогите Я написал скрипт и 2 часа не могу найти там ошибку
// Вы при помощи блокнота редактируете скрипты?
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GameController : MonoBehaviour
{
private CubePos nowCube = new CubePos(0, 1, 0);
private float cubeChangePlaceSpeed = 0.5f;
public Transform cubeToPlace;
private List< Vector3 > allCubesPositions = new List< Vector3 >
{
new Vector3(0,0,0),
new Vector3(1,0,0),
new Vector3(-1,0,0),
new Vector3(0,1,0),
new Vector3(0,0,1),
new Vector3(0,0,-1),
new Vector3(1,0,1),
new Vector3(-1,0,-1),
new Vector3(-1,0,1),
new Vector3(1,0,-1),
};
private void Start()
{
StartCoroutine(ShowCubePlace());
}
IEnumerator ShowCubePlace()
{
while (true)
{
SpawnPositions();
yield return new WaitForSeconds(cubeChangePlaceSpeed);
}
}
private void SpawnPositions()
{
List< Vector3 > positions = new List< Vector3 >();
if (IsPositionEmpty(new Vector3(nowCube.x + 1, nowCube.y, nowCube.z)) &&
nowCube.x + 1 != cubeToPlace.position.x)
positions.Add(new Vector3(nowCube.x + 1, nowCube.y, nowCube.z));
if (IsPositionEmpty(new Vector3(nowCube.x - 1, nowCube.y, nowCube.z)) &&
nowCube.x - 1 != cubeToPlace.position.x)
positions.Add(new Vector3(nowCube.x - 1, nowCube.y, nowCube.z));
if (IsPositionEmpty(new Vector3(nowCube.x, nowCube.y + 1, nowCube.z)) &&
nowCube.y + 1 != cubeToPlace.position.y)
positions.Add(new Vector3(nowCube.x, nowCube.y + 1, nowCube.z));
if (IsPositionEmpty(new Vector3(nowCube.x, nowCube.y - 1, nowCube.z)) &&
nowCube.y - 1 != cubeToPlace.position.y)
positions.Add(new Vector3(nowCube.x, nowCube.y - 1, nowCube.z));
if (IsPositionEmpty(new Vector3(nowCube.x, nowCube.y, nowCube.z + 1)) &&
nowCube.z + 1 != cubeToPlace.position.z)
positions.Add(new Vector3(nowCube.x, nowCube.y, nowCube.z + 1));
if (IsPositionEmpty(new Vector3(nowCube.x, nowCube.y, nowCube.z - 1)) &&
nowCube.z - 1 != cubeToPlace.position.z)
positions.Add(new Vector3(nowCube.x, nowCube.y, nowCube.z - 1));
cubeToPlace.position = positions[UnityEngine.Random.Range(0, positions.Count)];
Debug.Log(positions.Count);
}
private bool IsPositionEmpty(Vector3 targetPos)
{
if (targetPos.y == 0)
return false;
foreach (Vector3 pos in allCubesPositions)
{
if (pos.x == targetPos.x & pos.y == targetPos.y & pos.z == targetPos.z)
return false;
}
return true;
}
struct CubePos
{
public int x, y, z;
public CubePos(int x, int y, int z)
{
this.x = x;
this.y = y;
this.z = z;
}
public Vector3 getVector()
{
return new Vector3(x, y, z);
}
public void setVector(Vector3 pos)
{
x = Convert.ToInt32(pos.x);
y = Convert.ToInt32(pos.y);
z = Convert.ToInt32(pos.z);
}
}
}
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GameController : MonoBehaviour
{
private CubePos nowCube = new CubePos(0, 1, 0);
private float cubeChangePlaceSpeed = 0.5f;
public Transform cubeToPlace;
private List< Vector3 > allCubesPositions = new List< Vector3 >
{
new Vector3(0,0,0),
new Vector3(1,0,0),
new Vector3(-1,0,0),
new Vector3(0,1,0),
new Vector3(0,0,1),
new Vector3(0,0,-1),
new Vector3(1,0,1),
new Vector3(-1,0,-1),
new Vector3(-1,0,1),
new Vector3(1,0,-1),
};
private void Start()
{
StartCoroutine(ShowCubePlace());
}
IEnumerator ShowCubePlace()
{
while (true)
{
SpawnPositions();
yield return new WaitForSeconds(cubeChangePlaceSpeed);
}
}
private void SpawnPositions()
{
List< Vector3 > positions = new List< Vector3 >();
if (IsPositionEmpty(new Vector3(nowCube.x + 1, nowCube.y, nowCube.z)) &&
nowCube.x + 1 != cubeToPlace.position.x)
positions.Add(new Vector3(nowCube.x + 1, nowCube.y, nowCube.z));
if (IsPositionEmpty(new Vector3(nowCube.x - 1, nowCube.y, nowCube.z)) &&
nowCube.x - 1 != cubeToPlace.position.x)
positions.Add(new Vector3(nowCube.x - 1, nowCube.y, nowCube.z));
if (IsPositionEmpty(new Vector3(nowCube.x, nowCube.y + 1, nowCube.z)) &&
nowCube.y + 1 != cubeToPlace.position.y)
positions.Add(new Vector3(nowCube.x, nowCube.y + 1, nowCube.z));
if (IsPositionEmpty(new Vector3(nowCube.x, nowCube.y - 1, nowCube.z)) &&
nowCube.y - 1 != cubeToPlace.position.y)
positions.Add(new Vector3(nowCube.x, nowCube.y - 1, nowCube.z));
if (IsPositionEmpty(new Vector3(nowCube.x, nowCube.y, nowCube.z + 1)) &&
nowCube.z + 1 != cubeToPlace.position.z)
positions.Add(new Vector3(nowCube.x, nowCube.y, nowCube.z + 1));
if (IsPositionEmpty(new Vector3(nowCube.x, nowCube.y, nowCube.z - 1)) &&
nowCube.z - 1 != cubeToPlace.position.z)
positions.Add(new Vector3(nowCube.x, nowCube.y, nowCube.z - 1));
cubeToPlace.position = positions[UnityEngine.Random.Range(0, positions.Count)];
Debug.Log(positions.Count);
}
private bool IsPositionEmpty(Vector3 targetPos)
{
if (targetPos.y == 0)
return false;
foreach (Vector3 pos in allCubesPositions)
{
if (pos.x == targetPos.x & pos.y == targetPos.y & pos.z == targetPos.z)
return false;
}
return true;
}
struct CubePos
{
public int x, y, z;
public CubePos(int x, int y, int z)
{
this.x = x;
this.y = y;
this.z = z;
}
public Vector3 getVector()
{
return new Vector3(x, y, z);
}
public void setVector(Vector3 pos)
{
x = Convert.ToInt32(pos.x);
y = Convert.ToInt32(pos.y);
z = Convert.ToInt32(pos.z);
}
}
}
Может быть переменную создаешь несколько раз подрят с одним и темже названием
Похожие вопросы
- Пожалуйста помогите решить!!!
- С# пожалуйста помогите
- Code Review моего скрипта.
- Roblox studio,не могу сделать скрипт для поведения npc
- Roblox Studio скрипт
- Нужны скрипты в Unity 3д.
- Помощь с скриптом
- Скрипт на юнити 3д
- Скрипт на юнити 3д
- 2 Ошибки в uniti, 2 одной цифры, точнее cs1501, помогите, код представлен ниже visualStudio 2019