–1 vote

So I am working on a simple side-scrolling,dodge game where the player moves through the level(s) dodging objects coming at them. Visually, the game will feel like Flappy Bird, where the camera isn't focused on the player, but instead continuously moving on its own through the level and the player is having to keep up and not fall behind/not hit anything.

I have tried looking up documentation/tutorials/forums to see if anyone else has done something like this, but most google results are talking about the camera following the player, instead of the other way around. I have thought of maybe having a parallax background that repeats for the duration of the level and having the camera tied to that, or writing a camera2d script that moves the camera automatically to either the left or the right and the player has to move independently of it. So if the camera stops, and the players falls behind, they will fail the level.

I am still very new to Godot, so I'm still trying to get familiar with all the nodes and when and how to use them. Any help will be appreciated.

asked Jul 8 in Projects by bcalhoun15 (14 points)

1 Answer

0 votes
Best answer

A Camera2D is a Node2D and can be moved however you wish by changing its position.

answered Jul 8 by kidscancode (16,966 points)
selected Jul 10 by bcalhoun15

I wrote this script and attached it to my camera object in the scene, but its not moving it. The camera is set to Current and is not attached to the player at all.

using Godot;
using System;

public class CameraMovement : Godot.Camera2D
{
    [Export] public int speed = 40;
    public Camera2D camera = new Camera2D();

    public override void _Process(float delta)
    {
        var position = new Vector2(speed * delta, 0);
        camera.Position += position;
    }
}

new is making a new Camera2D object. If this script is attached to an existing Camera node, you just need to change Position directly.

that makes sense. I did this instead:

using Godot;
using System;

public class CameraMovement : Godot.Camera2D
{
    [Export] public int speed = 400;

    public override void _Process(float delta)
    {
        var velocity = new Vector2();
        velocity.x += 1;
        velocity = velocity * speed;

        Position += velocity * delta;
    }
}

but still not moving. I got this idea from looking at the first game tutorial on the Godot documentation. I am going to try printing the position to see exactly what is happening.

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 How to use this Q&A? before posting your first questions.