func draw_square():
var location = Vector3(round(self.position.x), 0.0, round(self.position.z))
var stored = false
if location != location_stored:
location_stored = location
if is_instance_valid(square):
print("delete")
square.queue_free()
drawn = false
if drawn != true:
make_square()
print("make_square")
drawn = true
func make_square():
var space_state = get_world_3d().direct_space_state
var square_ray = PhysicsRayQueryParameters3D.create(self.position, Vector3(self.position.x, self.position.y - 30, self.position.z))
var square_cast = space_state.intersect_ray(square_ray)
var square_location = square_cast.position.snapped(Vector3.ONE)
var neg_z_ray = PhysicsRayQueryParameters3D.create(Vector3(round(self.position.x), self.position.y + 1, round(self.position.z) - 0.49), Vector3(round(self.position.x), self.position.y - 30, round(self.position.z - 0.49)))
var pos_z_ray = PhysicsRayQueryParameters3D.create(Vector3(round(self.position.x), self.position.y + 1, round(self.position.z) + 0.49), Vector3(round(self.position.x), self.position.y - 30, round(self.position.z + 0.49)))
var neg_x_ray = PhysicsRayQueryParameters3D.create(Vector3(round(self.position.x) - 0.49, self.position.y + 1, round(self.position.z)), Vector3(round(self.position.x) - 0.49, self.position.y - 30, round(self.position.z)))
var pos_x_ray = PhysicsRayQueryParameters3D.create(Vector3(round(self.position.x) + 0.49, self.position.y + 1, round(self.position.z)), Vector3(round(self.position.x) + 0.49, self.position.y - 30, round(self.position.z)))
var neg_z_cast = space_state.intersect_ray(neg_z_ray)
var pos_z_cast = space_state.intersect_ray(pos_z_ray)
var neg_x_cast = space_state.intersect_ray(neg_x_ray)
var pos_x_cast = space_state.intersect_ray(pos_x_ray)
var scene_root = get_tree().root.get_children()[0]
square = MeshInstance3D.new()
var icon = QuadMesh.new()
square.mesh = icon
square.position = square_location + Vector3(0,0.01,0)
if neg_z_cast != {} && pos_z_cast != {}:
neg_z_location = neg_z_cast.position
pos_z_location = pos_z_cast.position
square.rotation.x = atan(neg_z_location.y - pos_z_location.y - 90)
if neg_x_cast != {} && pos_x_cast != {}:
neg_x_location = neg_x_cast.position
pos_x_location = pos_x_cast.position
square.rotation.z = atan(neg_x_location.y - pos_x_location.y)
var square_material = StandardMaterial3D.new()
square_material.set_texture(StandardMaterial3D.TEXTURE_ALBEDO, icon_texture)
square_material.transparency = BaseMaterial3D.TRANSPARENCY_ALPHA
icon.material = square_material
if square_cast.collider.is_in_group("ground"):
scene_root.add_child(square)
Material overlay might not be the correct solution for my problem. What i'm doing is simply creating a meshinstace3D quadmesh and instancing it at the player postion. This generally works correctly except for on slopes. If you look at my code, what I'm doing is attempting to shoot 4 rays down and find their positions where they collide with the floor. I then use trigonometry to determine the angles and rotate by those angles. But this doesnt seem to work and I'm not sure why.
I also get weird glitches every now and then when I first load the game. For some reason once I get a glitch one time, I won't get any more, but for example there might be an issue where this quadmesh does not appear at one space for some reason, but it will only happen one time and never again.