When I run the code, the area around the player which is within 5m from them intially is able to place blocks, which is handled under Placing Blocks however, when I move and attempt to place blocks outside the 5m radius the blocks dont place even tho I just copied and pasted the code under "REMOVING COLLIDERS ON ANY ENTITIES THAT ARE NOT NEAR" and put it in a function which is run whenever the player moves, which should work.
import math
from ursina import *
from ursina.prefabs.first_person_controller import FirstPersonController
from perlin_noise import PerlinNoise
import random
from distance_calc import distance_calc
noise = PerlinNoise(octaves=5, seed=random.randint(-1000000000000, 10000000000))
optimal_distance = 5
#set up app
app = Ursina()
# VARAIBLES
selected_block = "grass"
water = load_texture("assets/water.jpg"),
# TEXTURES
block_textures = {
'grass': load_texture("assets/grass.jpg"),
'dirt': load_texture("assets/dirt.jpg"),
'stone': load_texture("assets/stone.jpg"),
'bedrock': load_texture("assets/bedrock.jpg"),
'faux_bedrock': load_texture("assets/bedrock.jpg"),
'cobblestone': load_texture("assets/cobblestone.jpg")
}
# CREATE PLAYER
player = FirstPersonController(
mouse_sensitivity=Vec2(100, 100),
scale=0.85,
position=(0, 25, 0)
)
class Block(Entity):
def input(self, key):
if key == "w" or key == "a" or key == "s" or key == "d":
# UPDATING THE COLLIDERS WHEN PLAYER MOVES
self.distance_from_player = distance_calc(player.x, player.y, player.z, self.x, self.y, self.z)
if self.distance_from_player < optimal_distance:
self.collider = "box"
else:
self.collider = None
# UPDATING THE VIEWING WHEN PLAYER MOVES
if self.distance_from_player < 10:
self.visible = True
else:
self.visible = False
def __init__(self, position, block_type):
super().__init__(
position=position,
model="cube",
scale=1,
origin_y=-0.5,
texture=block_textures.get(block_type),
)
self.block_type = block_type
# OTIMIZATION
# REMOVING COLLIDERS ON ANY ENTITIES THAT ARE NOT NEAR
self.distance_from_player = distance_calc(player.x, player.y, player.z, self.x, self.y, self.z)
if self.distance_from_player < optimal_distance:
self.collider ="box"
else:
self.collider = None
# SETTING VIEWING DISTANCE TO 10 SO ANY ENTITIES FURTHER ARE NOT SHOWN
if self.distance_from_player < 10:
self.visible = True
else:
self.visible = False
mini_block = Entity(
parent=camera,
model="cube",
scale=0.2,
origin_y=-0.5,
texture=block_textures.get(selected_block),
collider="box",
position = (0.3, -0.25, 0.45),
)
# GROUND
ground = Entity(
model = 'plane',
scale = (100, 1, 100),
texture = "water",
texture_scale = (100, 100),
collider = "box",
block_type = "water",
position=(0,-5, 0),
)
#PLACING BLOCK
def input(key):
global selected_block
#place block
if key == "right mouse down":
hit_info = raycast(camera.world_position, camera.forward, distance=10)
if hit_info.hit:
block = Block(hit_info.entity.position + hit_info.normal, selected_block)
#place block
if key == "left mouse down" and mouse.hovered_entity:
if not mouse.hovered_entity.block_type == "bedrock":
if not mouse.hovered_entity.block_type == "water":
destroy(mouse.hovered_entity)
#BLOCK CHOOSER
if key=="1":
selected_block = "grass"
if key=="2":
selected_block = "stone"
if key=="3":
selected_block = "faux_bedrock"
if key=="4":
selected_block = "cobblestone"
# TERRAIN
min_height = -2
for x in range (-25, 25):
for z in range(-25, 25):
height = noise([x*0.02, z*0.02])
height = math.floor(height * 7.5)
for y in range(height, min_height -1, -1):
if y == min_height:
block = Block((x,y + min_height, z), "bedrock")
elif y == height:
block = Block((x,y + min_height, z), "grass")
elif height - y > 2:
block = Block((x,y + min_height, z), "stone")
else:
block = Block((x,y + min_height, z), "dirt")
def update():
mini_block.texture = block_textures.get(selected_block)
# RUN APP
app.run()