Godot Building System

85 views Asked by At

I have this Godot building script that ALMOST works. The issue is sometimes, blocks don't go exactly where I want them to. Does anyone have any solutions to this script to make it more accurate?

func _input(ev):
    if Input.is_mouse_button_pressed(MOUSE_BUTTON_RIGHT):
        var space_state = get_world_3d().direct_space_state
        var mousepos = get_viewport().get_mouse_position()
        var origin = camera.project_ray_origin(mousepos)
        var end = origin + camera.project_ray_normal(mousepos) * raycast_distance
        var query = PhysicsRayQueryParameters3D.create(origin, end)
        query.collide_with_areas = true
        var result = space_state.intersect_ray(query)
        if result && timer > 0.5:
            timer = 0
            var rigid_body = result.collider
            var hit_pos = result.position
            var hit_normal = result.normal
            print("Hit at point: ", result.position)
            var pos = hit_pos + hit_normal * snapValue
            pos = pos.ceil()
            print(hit_normal)
            if hit_normal.x == 1:
                pos += Vector3(-1, 0, 0)
                print ("1")
                
            elif hit_normal.z == 1:
                pos += Vector3(0, 0, -1)
                print ("2")
                
            elif hit_normal.y == 1:
                pos += Vector3(0, -1, 0)
                print ("1")
                
                
            result.collider.add_to_group("Grass", true)
            var grass = preload("res://Dirt.tscn")
            
            var clone = grass.instantiate()
            clone.name = "grass"
            add_sibling(clone)
            clone.position = pos
            

I tried changing the normal values, but it didn't seem to work...

0

There are 0 answers