27 lines
681 B
C#
27 lines
681 B
C#
using Godot;
|
|
using System;
|
|
|
|
public partial class camera : Camera2D
|
|
{
|
|
[Export] public Vector2 OffsetAmount = new Vector2(50, 70);
|
|
[Export] public float SmoothingSpeed = 0.05f;
|
|
// Called when the node enters the scene tree for the first time.
|
|
|
|
private CharacterBody2D _player;
|
|
|
|
public override void _Ready()
|
|
{
|
|
|
|
_player = GetParent<CharacterBody2D>();
|
|
}
|
|
|
|
public override void _Process(double delta)
|
|
{
|
|
Vector2 playerPos = _player.GlobalPosition;
|
|
Vector2 playerV = _player.Velocity;
|
|
Vector2 direction = playerV.Normalized();
|
|
Vector2 cameraTargetPos = playerPos + OffsetAmount * direction;
|
|
GlobalPosition = GlobalPosition.Lerp(cameraTargetPos, SmoothingSpeed);
|
|
}
|
|
}
|