如何修复 Godot 中的 'invalid set index position on base kinematic body' 错误?

How to fix 'invalid set index position on base kinematic body' error in Godot?

在 Godot 中,我试图让一个运动体传送到鼠标的位置,但我得到了这个错误:Invalid set index 'position' (on base: 'KinematicBody') with value of type 'Vector3'.我不想使用移动和滑动,因为我想要这个项目被传送到鼠标,而不是向鼠标移动

我的代码:

extends KinematicBody

# Declare member variables here. Examples:
# var a = 2
# var b = "text"

# Called when the node enters the scene tree for the first time.
func _ready():
    pass # Replace with function body.

# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta):
    if (Input.is_action_pressed("ui_right")):
        rotate_y(-0.1)
    elif (Input.is_action_pressed("ui_left")):
        rotate_y(0.1)
    elif (Input.is_action_pressed("ui_up")):
        rotate_z(0.1)
    elif (Input.is_action_pressed("ui_down")):
        rotate_z(-0.1)
    var tempPosition = place()
    print(tempPosition)
    if tempPosition != null:
        var temp_mesh  =get_tree().get_root().get_node("World/TestBody")
        temp_mesh.position = tempPosition


func _input(event):
    if event is InputEventMouseMotion:
        rotate_y(-event.relative.x * 0.0002)
        $pivot.rotate_x(-event.relative.y * 0.0002)
        $pivot.rotation.x = clamp($pivot.rotation.x, -1.2, 1.2)

# Place an item
func place():
    var ray_length = 5000
    var mouse_pos = get_viewport().get_mouse_position()
    var camera = get_node("pivot/Camera")
    var from = camera.project_ray_origin(mouse_pos)
    var to = from + camera.project_ray_normal(mouse_pos) * ray_length
    var space_state = get_world().get_direct_space_state()
    var result = space_state.intersect_ray(from, to)
    if result:
        return result.position

KinematicBody 没有 position 属性(只有 KinematicBody2D 有)。对于像 KinematicBody 这样的 3D Spatial 对象,你应该使用 global_transform.origin.