0

在 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
4

1 回答 1

0

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

于 2019-08-31T11:13:01.317 回答