things/orbiters/Orbiter.cs

A Thing-derived component that orbits another Unit. It stores orbit parameters, updates its angle each tick, and sets its world position based on the orbited unit's 2D position and a fixed Z offset.

using System;
using Sandbox;

public class Orbiter : Thing
{
	[Property] public ModelRenderer Model { get; set; }

	[Property, Hide] public Unit OrbitedUnit { get; set; }

	public float OrbitSpeed { get; set; }
	public float OrbitDistance { get; set; }
	public float CurrOrbitAngleDegrees { get; set; }

	protected override void OnStart()
	{
		base.OnStart();

		if ( IsProxy )
			return;

	}

	protected override void OnUpdate()
	{
		base.OnUpdate();

		if ( IsProxy )
			return;

		if( !OrbitedUnit.IsValid() || OrbitedUnit.IsDying )
			return;

		var pos = OrbitedUnit.Position2D + Utils.GetVector2FromAngleDegrees( CurrOrbitAngleDegrees ) * OrbitDistance;
		CurrOrbitAngleDegrees += OrbitSpeed * Time.Delta;

		WorldPosition = new Vector3( pos.x, pos.y, GetZPos() );
	}

	protected virtual float GetZPos()
	{
		return OrbitedUnit.WorldPosition.z + 35f;
	}
}