I’m trying to make a gungeon like game and I’m having trouble creating a camera. I can’t find any guides. Below I’ll attach the code I have now, but it’s not quite like what’s in Enter The Gungeon.
Just a friendly suggestion: your question is likely to attract more answers if you explain in detail what you want your camera to do and how your current implementation differs. As is, you’re limiting potential respondents to those who have played Enter the Gungeon and are familiar with its camera. (I haven’t played that game, so I can’t help.)
(Also, please format your code by enclosing it with three backticks (```).)
Okay i try to explain what I want from the camera. I am making a 2d top down shooter game and i need the camera not to move if the mouse cursor is close to the player, 10-20 pixels, and as the mouse cursor moves away from the player, the camera should move a little towards the cursor. Also, the camera should have a limit on how far it can move from the player, about 60 pixels.
And here the code
extends Camera2D
const MAX_DISTANCE = 48
var target_distance = 0
var center_pos = position
func _process(delta):
var direction = center_pos.direction_to(get_local_mouse_position())
var target_pos = center_pos + direction * target_distance
target_pos = target_pos.clamp(
center_pos - Vector2(MAX_DISTANCE, MAX_DISTANCE),
center_pos + Vector2(MAX_DISTANCE,MAX_DISTANCE)
)
position = target_pos
func _input(event):
if event is InputEventMouseMotion:
target_distance = center_pos.distance_to(get_local_mouse_position()) / 1.3
It kind of seems like you want something like:
# untested pseudocode...
extends Camera2D
var player # Reference to the player
var target_pos: Vec2 # Where we want to be.
func _process(delta):
# Vector from player pos to mouse pos.
var vec = get_local_mouse_position() - player.position
# Length of vector.
var len = vec.length()
if len < MIN_DISTANCE:
target_pos = player.position
elif len > MAX_DISTANCE:
vec = vec.normalized() * MAX_DISTANCE # Clamp
target_pos = player.position + vec
else:
target_pos = player.position + vec
# interpolate position towards target_position
vec interp_vec = target_position - position
if interp_vec.length() < SNAP_DISTANCE: # some small value
position = target_position
else:
position += (interp_vec * 0.5) # Cover half the distance.