How to make a Camera move independently of the player object

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By bcalhoun15

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.

:bust_in_silhouette: Reply From: kidscancode

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

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;
	}
}

bcalhoun15 | 2020-07-09 18:41

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

kidscancode | 2020-07-09 18:44

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.

bcalhoun15 | 2020-07-10 16:09