0 votes

Hi!

I want to use 3D background (instead of parallax layers) in my 2D scene.
So I have a simple 2D scene (with player, Camera2D, platforms... etc.) and a CanvasLayer "background" that contains 3D objects (meshes, 3d camera... etc.).
When the player moves I want to change the 3D background's cam position too!

I can do this with the camera's set_translation function.
Now in my code if the player (and the 2D camera) moves 1 pixel in 2D, then the 3D camera moves 1 unit (???) in 3D.
In this way the 3D movement is too fast.
Of course I can decrease the values (ie. 0.5), but I don't know the "right calculation formula".

I uploaded the sample project: google drive

Screenshot:
enter image description here

The script:

extends Sprite

func _process(delta):
    var sprite2dPos = get_position();
    var cam3d = get_parent().get_node("3dLayer/spatial/cam3d");
    var trans3d = cam3d.get_translation();

    if(Input.is_action_pressed("ui_right")):
        sprite2dPos.x += 1;
        trans3d.x += 1;
    elif(Input.is_action_pressed("ui_left")):
        sprite2dPos.x -= 1;
        trans3d.x -= 1;

    if(Input.is_action_pressed("ui_up")):
        sprite2dPos.y -= 1;
        trans3d.y += 1;
    elif(Input.is_action_pressed("ui_down")):
        sprite2dPos.y += 1;
        trans3d.y -= 1;

    set_position(sprite2dPos);
    cam3d.set_translation(trans3d);

Can you help me, how to calculate the 3D camera's position correctly?

(Godot 3.2.1 standard 64bit, win10)

Thank you!

in Engine by (674 points)
retagged by

1 Answer

0 votes

I'll try to rephrase my question (hard to explain... sorry).
I drew a simple example:
enter image description here
The distance is e.g. 10 pixels in 2D, and I need the corresponding distance in 3D.

How can I calulate this?

Thank you!

by (674 points)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.