Attention | Topic was automatically imported from the old Question2Answer platform. | |
Asked By | inaruslynx |
My camera had been working fine, but I had an issue with draw order. So it solve the problems I was having, I added CanvasLayers (Background, Foreground, and UI) and organized all of my scenes accordingly. I left the Camera where it was in the main parent tree. The camera is controlled via a script.
here is the code:
extends Camera2D
@export var backgroundColor :Color
@export var shakeNoise : FastNoiseLite
var targetPosition = Vector2.ZERO
var xNoiseSampleVector = Vector2.RIGHT
var yNoiseSampleVector = Vector2.DOWN
var xNoiseSamplePosition = Vector2.ZERO
var yNoiseSamplePosition = Vector2.ZERO
var noiseSampleTravelRate = 300
var maxShakeOffeset = 7
var currentShakePercentage = 0
var shakeDecay = 3
# Called when the node enters the scene tree for the first time.
func _ready():
RenderingServer.set_default_clear_color(backgroundColor)
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta):
acquire_target_position()
global_position = lerp(global_position, targetPosition, pow(2, -15 * delta))
if currentShakePercentage > 0:
xNoiseSamplePosition += xNoiseSampleVector * noiseSampleTravelRate * delta
yNoiseSamplePosition += yNoiseSampleVector * noiseSampleTravelRate * delta
var xSample = shakeNoise.get_noise_2d(xNoiseSamplePosition.x, xNoiseSamplePosition.y)
var ySample = shakeNoise.get_noise_2d(yNoiseSamplePosition.x, yNoiseSamplePosition.y)
var calculatedOffset = Vector2(xSample, ySample) * maxShakeOffeset * pow(currentShakePercentage,2)
offset = calculatedOffset
currentShakePercentage = clamp(currentShakePercentage - shakeDecay * delta, 0, 1)
func acquire_target_position():
var acquired = get_target_position_from_node("player")
if !acquired:
get_target_position_from_node("player_death")
func get_target_position_from_node(groupName):
var nodes = get_tree().get_nodes_in_group(groupName)
for node in nodes:
targetPosition = node.global_position
return true
return false
func apply_shake(percentage):
currentShakePercentage = clamp(currentShakePercentage + percentage, 0, 1)
I did a print(global_position) in the func _process(delta), and the value changes as my character is moved. But for whatever reason the camera just sits at -180, -144, which is the center of a blue box in the editor. I can only assume the camera isn’t moving because of some relative position setting and it broke because I moved everything into CanvasLayers. I really don’t want to undo my CanvasLayers.