Skip to content

Commit

Permalink
-Infinite World generation
Browse files Browse the repository at this point in the history
-Player Movement changes
  • Loading branch information
7ubi committed May 9, 2021
1 parent 4af52a3 commit a0a5c23
Show file tree
Hide file tree
Showing 7 changed files with 576 additions and 30 deletions.
14 changes: 14 additions & 0 deletions Assets/Player.physicMaterial
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
%YAML 1.1
%TAG !u! tag:unity3d.com,2011:
--- !u!134 &13400000
PhysicMaterial:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_Name: Player
dynamicFriction: 1
staticFriction: 1
bounciness: 0
frictionCombine: 0
bounceCombine: 0
8 changes: 8 additions & 0 deletions Assets/Player.physicMaterial.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 9 additions & 8 deletions Assets/Scenes/SampleScene.unity
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,8 @@ MonoBehaviour:
maxHeight: 15
seed: 69
Chunck: {fileID: 8451236987504973569, guid: 320c3e7c5fc352247932ecdea37a94a4, type: 3}
renderDistance: 6
Player: {fileID: 1743702924}
--- !u!4 &1731320883
Transform:
m_ObjectHideFlags: 0
Expand All @@ -358,29 +360,28 @@ GameObject:
- component: {fileID: 1743702928}
- component: {fileID: 1743702927}
- component: {fileID: 1743702926}
- component: {fileID: 1743702925}
- component: {fileID: 1743702929}
- component: {fileID: 1743702930}
- component: {fileID: 1743702925}
m_Layer: 0
m_Name: Player
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
--- !u!136 &1743702925
CapsuleCollider:
--- !u!65 &1743702925
BoxCollider:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1743702924}
m_Material: {fileID: 0}
m_Material: {fileID: 13400000, guid: 54b0ea5ba996ad447b7c81656f7a508c, type: 2}
m_IsTrigger: 0
m_Enabled: 1
m_Radius: 0.5
m_Height: 2
m_Direction: 1
serializedVersion: 2
m_Size: {x: 1, y: 2, z: 1}
m_Center: {x: 0, y: 0, z: 0}
--- !u!23 &1743702926
MeshRenderer:
Expand Down Expand Up @@ -475,7 +476,7 @@ MonoBehaviour:
m_Script: {fileID: 11500000, guid: 74bd70de0451f3c41bcc93f80c2062ea, type: 3}
m_Name:
m_EditorClassIdentifier:
moveSpeed: 5
moveSpeed: 4
_camera: {fileID: 963194227}
speedH: 2
speedV: 2
Expand Down
18 changes: 15 additions & 3 deletions Assets/Scripts/PlayerController.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
Expand All @@ -10,6 +11,9 @@ public class PlayerController : MonoBehaviour
[SerializeField] private float speedV = 2.0f;
[SerializeField] private float jumpForce;

private float forward;
private float right;

private float _yaw = 0.0f;
private float _pitch = 0.0f;

Expand All @@ -24,9 +28,8 @@ private void Start()

private void Update()
{
var forward = Input.GetAxis("Vertical") * moveSpeed;
var right = Input.GetAxis("Horizontal") * moveSpeed;
transform.position += transform.forward * (forward * Time.deltaTime) + transform.right * (right * Time.deltaTime);
forward = Input.GetAxis("Vertical") * moveSpeed;
right = Input.GetAxis("Horizontal") * moveSpeed;

var mouseRight = Input.GetAxis("Mouse X");
var mouseUp = Input.GetAxis("Mouse Y");
Expand All @@ -46,4 +49,13 @@ private void Update()
if (Input.GetKey(KeyCode.Space) && _rb.velocity.y == 0)
_rb.velocity = new Vector3(_rb.velocity.x, jumpForce, _rb.velocity.z);
}

private void LateUpdate()
{
var velocity = _rb.velocity;
var yVel = velocity.y;
velocity = transform.forward * forward + transform.right * right;
velocity = new Vector3(velocity.x, yVel, velocity.z);
_rb.velocity = velocity;
}
}
73 changes: 54 additions & 19 deletions Assets/Scripts/world_creation.cs
Original file line number Diff line number Diff line change
@@ -1,31 +1,40 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using UnityEngine;

public class world_creation : MonoBehaviour
{
//base code: https://github.com/Absurdponcho/BlockGame/blob/master/Assets/VoxelChunk.cs
[SerializeField] int Size = 16;
[SerializeField] private int maxHeight;
public short[,,] BlockIDs;
private bool RequiresMeshGeneration = false;
private short[,,] BlockIDs;

[SerializeField] private int seed;
[SerializeField] private GameObject Chunck;

[SerializeField] private float renderDistance;
[SerializeField] private GameObject Player;
private List<GameObject> chuncks = new List<GameObject>();
private int _lastChunck = 0;

private void Start()
{
GenerateChunck();
}

private void Update()
{
if (RequiresMeshGeneration)
var position = Vector3Int.FloorToInt(Player.transform.position);
var currentChunck = (position.x - (position.x % 8) + position.z - (position.z % 8));
if (currentChunck != _lastChunck)
{
GenerateChunck();
}
}

private void Start()
{

RequiresMeshGeneration = true;
_lastChunck = currentChunck;
}

void GenerateBlocks(Vector2 offset)
Expand All @@ -35,7 +44,7 @@ void GenerateBlocks(Vector2 offset)
{
for (var z = 0; z < Size; z++)
{
var height = (int)(Mathf.PerlinNoise((x + offset.x * 2) * 0.05f + seed, (z + offset.y * 2) * 0.05f + seed) * (maxHeight));
var height = (int)(Mathf.PerlinNoise((x + offset.x) * 0.05f + seed, (z + offset.y) * 0.05f + seed) * (maxHeight));
for(var y = height; y >= 0; y--){
BlockIDs[x, y, z] = 1;
}
Expand All @@ -44,20 +53,49 @@ void GenerateBlocks(Vector2 offset)
}
}

void GenerateChunck()
private void GenerateChunck()
{
for (var x = 0; x < 3; x++)
var position = Player.transform.position;
var playerX = position.x - (position.x % 16);
var playerZ = position.z - (position.z % 16);

var minX = Convert.ToInt32(playerX - Size * renderDistance);
var maxX = Convert.ToInt32(playerX + Size * renderDistance);
var minZ = Convert.ToInt32(playerZ - Size * renderDistance);
var maxZ = Convert.ToInt32(playerZ + Size * renderDistance);

for (var x = minX; x <= maxX; x += Size)
{
for (var z = 0; z < 3; z++)
for (var z = minZ; z <= maxZ; z += Size)
{
var chunck = Instantiate(Chunck, new Vector3(x * Size * 0.5f, 0, z * Size * 0.5f), Quaternion.identity);
GenerateMesh(chunck);
var exists = chuncks.Any(chunck =>
{
Vector3 position1;
return Math.Abs((float) ((position1 = chunck.transform.position).x - x)) < 0.1f && Math.Abs((float) (position1.z - z)) < 0.1f;
});

if (exists) continue;
var newChunck = Instantiate(Chunck, new Vector3(x, 0, z), Quaternion.identity);
GenerateMesh(newChunck);
chuncks.Add(newChunck);
}
}

if(chuncks.Count == 0)
return;

for(var i = chuncks.Count - 1; i >= 0; i--)
{
var chunck = chuncks[i];
if (!(chunck.transform.position.x < minX) && !(chunck.transform.position.x > maxX) &&
!(chunck.transform.position.z < minZ) && !(chunck.transform.position.z > maxZ)) continue;
chuncks.Remove(chunck);
Destroy(chunck);
}
}

// ReSharper disable Unity.PerformanceAnalysis
void GenerateMesh(GameObject chunck)
private void GenerateMesh(GameObject chunck)
{
var position = chunck.transform.position;
GenerateBlocks(new Vector2(position.x, position.z));
Expand All @@ -75,7 +113,7 @@ void GenerateMesh(GameObject chunck)
{
for (var z = 0; z < Size; z++)
{
var offset = new Vector3Int(x, y, z) + Vector3Int.FloorToInt(chunck.transform.position);
var offset = new Vector3Int(x, y, z);
if (BlockIDs[x, y, z] == 0) continue;
else
{
Expand Down Expand Up @@ -163,9 +201,6 @@ void GenerateMesh(GameObject chunck)
newMesh.RecalculateTangents();
chunck.GetComponent<MeshFilter>().mesh = newMesh;
chunck.GetComponent<MeshCollider>().sharedMesh = newMesh;
// Set texture

RequiresMeshGeneration = false;
}

void GenerateBlock_Top(ref int currentIndex, Vector3Int offset, List<Vector3> vertices, List<Vector3> normals, List<Vector2> uvs, List<int> indices, Rect blockUVs)
Expand Down
Loading

0 comments on commit a0a5c23

Please sign in to comment.