Godot 建筑系统

问题描述 投票:0回答:1

我有这个 Godot 构建脚本,几乎可以工作。问题是有时,块不会完全到达我想要的位置。有谁对此脚本有任何解决方案以使其更准确吗?

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
            

我尝试更改正常值,但似乎不起作用...

godot gdscript building
1个回答
0
投票

我不明白你的“pos”变量以及你试图通过将法线添加到世界碰撞点来做什么。

如果您尝试将草定向到碰撞面,那么您应该基于该法线和碰撞位置构建变换。使用法向量旋转草对象的基础,并以碰撞位置作为变换原点。

否则请澄清您的问题。

© www.soinside.com 2019 - 2024. All rights reserved.