Get Viewport camera, nothing works

Godot Version

4.3

Question

I’ve tried it all, just not the thing that works. Internet has no answer for me, chatgpt can’t help, now I put faith in the local wizards!

I’m trying to get a ray cast from the center of the editor viewport and out, while in the editor mode and not in game.

@tool ofcourse.

Godot keeps saying everything from all the solutions and suggestions so far are wrong or worse: simply does not exist in godot.

How to even get the position and rotation of the viewport camera yields nothing. Just 000 and 000.

Get_viewport().get_camera_3d().global_position
Nothing

Does anyone have a current working snippet that Does work? :cry::cry::cry:

Have you tried this?

var viewport := EditorInterface.get_editor_viewport_3d(0)

I ll try. Seems alot of posts are out of date. The added _3d has thrown me off in other instances. Thanks.

this works for me… now.


extends EditorScript

func getViewport():
	if Engine.is_editor_hint():
		var editor_interface = get_editor_interface()
		var editor_viewport = editor_interface.get_editor_viewport_3d(0)
		var viewport_position = editor_viewport.get_camera_3d().global_position
1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.