b3agz / code-a-game-like-minecraft-in-unity Goto Github PK
View Code? Open in Web Editor NEWProject files for a Youtube tutorial series on coding a game like Minecraft in Unity.
Project files for a Youtube tutorial series on coding a game like Minecraft in Unity.
This loop generates 6 vertices per face when it should be 4 vertices because 2 triangles on one cube face should share 2 vertices.
Im having some serious trouble on Tutorial 5 right now. Whenever i start a game, the player keeps spawning too high no matter what i do. Please respond immediately, because i don't have patience right now.
At the beginning, the player's position is 0, 0, 0.
When playing, the player's position is 800, 130, 800
Same issue with the spawn position. Default values are 0, 0, 0.
When playing, the spawn position is set to 800, 130, 800
I dont even know what the heck i did wrong, so respond even if you're busy
Hey, i can't thank you enough for this tutorial!
I am not sure why, but after Cross chunk lighting tutorial when i start the game my RAM usage goes to almost 100%
Even after i manage to press stop in editor, it stays at 98%
I tested it in build and it behaves the same.
I thought that probably i messed something up, but when i download your project, it does the same thing.
Not sure if something was missed, i sure as hell can't find it xd
Anyway, just wanted to point this out since i can't continue after this one. I cant test it or do anything even
Again, thank you so much for doing this series, i learned so much from it!
In chunk.cs CreateMesh, before assigning the new mesh to meshFIlter (meshFilter.mesh = mesh;
), we have to destroy the old mesh (Object.Destroy(meshFilter.sharedMesh);
); otherwise we will face memory leak. The leak is disastrous especially when you put lots of blocks in game (>1000).
Hello, under what license is this project released under ? I would like to study it to learn from it . Thank you.
Like the title in episode 22 you changed the name of the main scene back to World but never changed in inside the TitleMenu.cs code, line 53:
public void StartGame() {
VoxelData.seed = Mathf.Abs(seedField.text.GetHashCode()) / VoxelData.WorldSizeInChunks;
SceneManager.LoadScene("main", LoadSceneMode.Single);
}
Should be:
public void StartGame() {
VoxelData.seed = Mathf.Abs(seedField.text.GetHashCode()) / VoxelData.WorldSizeInChunks;
SceneManager.LoadScene("World", LoadSceneMode.Single);
}
I Got Suck At Ep 03 Can You Pls Help
i got that error, what should i do?
Can I have the reference please?
I'm currently at part 12-Treading, and I've got a weird issue. I did everything as it's said, my code is exactly like in the provided assets folder, but if I don't lock the modifications Queue in this line:
modifications.Enqueue(Structure.MakeTree(pos, biome.minTreeHeight, biome.maxTreeHeight));
then I get a NullReferenceException when I Dequeue (line 141 in the provided script), and there are nearly no trees in the map, except for a few that are farer away from the player. The Queue that I get is null.
If I lock the Queue, this problem doesn't appear, but then I can't destroy the blocks of the trees.
Even though I used the code linked here it is giving me errors, specifically the player and world scripts, and I have no idea why. Please help.
Love your tutorial, but I'm struggling to get my voxels to appear, I'm on part 4 right now, my textures seemed to work okay for part 3, but now nothing shows up, I cloned the repository, and it appears this is a bug with the source scripts and I don't know how to fix it. If all of the blocks are the same type it runs fine, but when I fill the materials list with the 12 blocks on the texture sheet, everything disappears, but no errors are thrown.
I have the textures set up as follows
Size: 12
Everything is solid
Stone:
0,0,0,0,0,0
Dirt:
1,1,1,1,1,1
Grass:
2,2,7,1,2,2
Coal:
3,3,3,3,3,3
Planks:
4,4,4,4,4,4
Log:
5,5,6,6,5,5
Cobble:
8,8,8,8,8,8
Bedrock:
9,9,9,9,9,9
Sand:
10,10,10,10,10,10
Brick:
11,11,11,11,11,11
Furnace:
13,12,15,15,13,13
Lit Furnace:
13,14,15,15,13,13
I'm using the source scripts straight from part 4, I went back and swapped my code out for the original and the problem persists.
IndexOutOfRangeException: Index was outside the bounds of the array.
Chunk.CheckVoxel (UnityEngine.Vector3 pos) (at Assets/Chunk.cs:71)
Chunk.AddVoxelDataToChunk (UnityEngine.Vector3 pos) (at Assets/Chunk.cs:79)
Chunk.CreateMeshData () (at Assets/Chunk.cs:54)
Chunk.Start () (at Assets/Chunk.cs:24)
I am getting the following message while I am trying to attach World.cs and Chunk,cs script into the respective game objects.
"The script don't inherit a native class that can manage a script"
Please suggest what needs to be done.
Code_Chunk.cs.txt
Code_World.cs.txt
Thanks in advance
Hi, im following this tutorial, but in my case i have a huge flat ground. the problem here is that ground textures do not apear correctly in distance. i have posted a question here, will you please help me resolve this issue?
Hello ! Thanks for this tutorial ! It's really great !!
I think there is a problem in this part of the code.
As you said in one of your video, when you remove an element from the list, the indexes are recalculated.
In your checkViewDistance method, you use an index to find the chunk that needs to be kept. that's ok, but why does the for loop goes on (with the indexes recalculated) once the chunk is found ?
In my opinion, it you be better if there was this code : (with a break once the chunk found)
for (int i=0; i< previouslyActiveChunks.Count; i++) {
if (previouslyActiveChunks[i].Equals(chunks[x, z].coord)) {
previouslyActiveChunks.RemoveAt(i);
break;
}
}
Or even better, as the method Equals exists for the ChunkCoord class :
previouslyActiveChunks.Remove(chunks[x, z].coord);
Thanks again for your videos !! A real pleasure to watch !!
Cheers from France !
I followed the code EXACTLY. Then when that didn't work, I copied and pasted the code. One corner of the mesh does not render leaving me with a misshapen blob.
Edit: I recopied the code for the voxeldata. It Now works
My debug screen is only showing the name nothing else
yet it says in the text area what it is.
https://imgur.com/a/6Hd7DTa
Hey, I have a quick suggestion, something that I did as I'm following your series. I went ahead and created a BlockIndexes
enum so that I could refer to my blocks by name rather than trying to remember what byte they were assigned to. So when adding dirt, I add (byte) BlockIndexes.Dirt
. I don't know if it'll help or not, but it might be a nice addition to make the code a bit more readable
THIS IS WHATS HAPPENING HERES THE CODE FOR WORLD-
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class World : MonoBehaviour {
public Transform player;
public Vector3 spawn;
public Material material;
public BlockType[] blocktypes;
Chunk[,] chunks = new Chunk[VoxelData.WorldSizeInChunks, VoxelData.WorldSizeInChunks];
List<ChunkCoord> activeChunks = new List<ChunkCoord>();
ChunkCoord playerLastChunkCoord;
private void Start() {
GenerateWorld();
playerLastChunkCoord = GetChunkCoordFromVector3(player.transform.position);
}
private void Update() {
if (!GetChunkCoordFromVector3(player.transform.position).Equals(playerLastChunkCoord))
CheckViewDistance();
}
ChunkCoord GetChunkCoordFromVector3 (Vector3 pos) {
int x = Mathf.FloorToInt(pos.x / VoxelData.ChunkWidth);
int z = Mathf.FloorToInt(pos.z / VoxelData.ChunkWidth);
return new ChunkCoord(x, z);
}
private void GenerateWorld () {
for (int x = VoxelData.WorldSizeInChunks / 2 - VoxelData.ViewDistanceInChunks / 2; x < VoxelData.WorldSizeInChunks / 2 + VoxelData.ViewDistanceInChunks / 2; x++) {
for (int z = VoxelData.WorldSizeInChunks / 2 - VoxelData.ViewDistanceInChunks / 2; z < VoxelData.WorldSizeInChunks / 2 + VoxelData.ViewDistanceInChunks / 2; z++) {
CreateChunk(new ChunkCoord(x, z));
}
}
spawn = new Vector3(VoxelData.WorldSizeInBlocks / 2, VoxelData.ChunkHeight + 2, VoxelData.WorldSizeInBlocks / 2);
player.position = spawn;
}
private void CheckViewDistance () {
int chunkX = Mathf.FloorToInt(player.position.x / VoxelData.ChunkWidth);
int chunkZ = Mathf.FloorToInt(player.position.z / VoxelData.ChunkWidth);
List<ChunkCoord> previouslyActiveChunks = new List<ChunkCoord>(activeChunks);
for (int x = chunkX - VoxelData.ViewDistanceInChunks / 2; x < chunkX + VoxelData.ViewDistanceInChunks / 2; x++) {
for (int z = chunkZ - VoxelData.ViewDistanceInChunks / 2; z < chunkZ + VoxelData.ViewDistanceInChunks / 2; z++) {
// If the chunk is within the world bounds and it has not been created.
if (IsChunkInWorld(x, z)) {
ChunkCoord thisChunk = new ChunkCoord(x, z);
if (chunks[x, z] == null)
CreateChunk(thisChunk);
else if (!chunks[x, z].isActive) {
chunks[x, z].isActive = true;
activeChunks.Add(thisChunk);
}
// Check if this chunk was already in the active chunks list.
for (int i = 0; i < previouslyActiveChunks.Count; i++) {
//if (previouslyActiveChunks[i].Equals(new ChunkCoord(x, z)))
if (previouslyActiveChunks[i].x == x && previouslyActiveChunks[i].z == z)
previouslyActiveChunks.RemoveAt(i);
}
}
}
}
foreach (ChunkCoord coord in previouslyActiveChunks)
chunks[coord.x, coord.z].isActive = false;
}
bool IsChunkInWorld(int x, int z) {
if (x > 0 && x < VoxelData.WorldSizeInChunks - 1 && z > 0 && z < VoxelData.WorldSizeInChunks - 1)
return true;
else
return false;
}
private void CreateChunk (ChunkCoord coord) {
chunks[coord.x, coord.z] = new Chunk(new ChunkCoord(coord.x, coord.z), this);
activeChunks.Add(new ChunkCoord(coord.x, coord.z));
}
public byte GetVoxel (Vector3 pos) {
if (pos.x < 0 || pos.x > VoxelData.WorldSizeInBlocks - 1 || pos.y < 0 || pos.y > VoxelData.ChunkHeight - 1 || pos.z < 0 || pos.z > VoxelData.WorldSizeInBlocks - 1)
return 0;
if (pos.y < 1)
return 1;
else if (pos.y == VoxelData.ChunkHeight - 1)
return 3;
else
return 2;
}
}
public class ChunkCoord {
public int x;
public int z;
public ChunkCoord (int _x, int _z) {
x = _x;
z = _z;
}
public bool Equals(ChunkCoord other) {
if (other == null)
return false;
else if (other.x == x && other.z == z)
return true;
else
return false;
}
}
[System.Serializable]
public class BlockType {
public string blockName;
public bool isSolid;
[Header("Texture Values")]
public int backFaceTexture;
public int frontFaceTexture;
public int topFaceTexture;
public int bottomFaceTexture;
public int leftFaceTexture;
public int rightFaceTexture;
// Back, Front, Top, Bottom, Left, Right
public int GetTextureID (int faceIndex) {
switch (faceIndex) {
case 0:
return backFaceTexture;
case 1:
return frontFaceTexture;
case 2:
return topFaceTexture;
case 3:
return bottomFaceTexture;
case 4:
return leftFaceTexture;
case 5:
return rightFaceTexture;
default:
Debug.Log("Error in GetTextureID; invalid face index");
return 0;
}
}
}
Whenever I try to run the script, i get the following warning and nothing generates
"You are trying to create a MonoBehaviour using the 'new' keyword. This is not allowed. MonoBehaviours can only be added using AddComponent(). Alternatively, your script can inherit from ScriptableObject or no base class at all
UnityEngine.MonoBehaviour:.ctor()
Chunk:.ctor(ChunkCoord, World) (at Assets/Scripts/Chunk.cs:23)
World:CreateChunk(ChunkCoord) (at Assets/Scripts/World.cs:119)
World:GenerateWorld() (at Assets/Scripts/World.cs:51)"
I've followed your two tutorials and they were really great. I was hoping you could start them up again. They've really helped me out and a lot of others who've watched those videos
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.