如何修复Godot中的“​​基本运动体上无效的设置索引位置”错误?

时间:2019-08-31 09:13:25

标签: godot gdscript

在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

1 个答案:

答案 0 :(得分:0)

KinematicBody没有position属性(只有KinematicBody2D有)。对于KinematicBody之类的3D Spatial对象,应使用global_transform.origin