Search code examples
cameraviewportgodot

Godot Viewport Node won't move with Parent Node


I am trying to have a Sprite 3D have a Viewport texture and I set up my Nodes like this: enter image description here When I added Portal1 to another scene and moved it, the everything moved except the Viewport Node and Everything under it: Before:enter image description here After:enter image description here

As you can see the Camera didn't move and I figured out it's because the Viewport didn't move. I have tried multiple combinations and nothing works. How can I get the Viewport node to move with the Parent?


Solution

  • There is no way to structure the scene tree that will do what you want. The reason being that the Viewport is the root of the transformations. The Viewport itself does not have a transformation (so it does not makes sense to speak of the moving the Viewport), and Godot does not resolve transformations pass a Viewport.

    What you can do is have the your scene root copy its global_transform. Something like this (if it is intended to be moving all the time):

    extends Spatial
    
    
    onready var root := $Viewport/Root as Spatial
    
    
    func _process(_delta: float) -> void:
        root.global_transform = global_transform
    

    Or like this (which is better if it rarely moves):

    extends Spatial
    
    
    onready var root := $Viewport/Root as Spatial
    
    
    func _ready() -> void:
        set_notify_transform(true)
    
    
    func _notification(what: int) -> void:
        if what == NOTIFICATION_TRANSFORM_CHANGED:
            root.global_transform = global_transform
    

    And you can make it a tool script if you want it to work on the editor. Be aware that you will need to reload the scene in the editor for it to begin working.

    With a scene tree something like this:

    Portal
    ├ Viewport
    │ └ Root
    │   └ Camera
    └ Sprite3D
    

    With the script attached to Portal.

    Note that I'm not copying the global_transform to the Camera but to a new Node which I called Root so you can still move the Camera relative to it.