things/orbiters/Orbiter.cs

A Thing subclass that represents an orbiter object which rotates around a Unit. It stores orbit speed, distance and current angle, updates its world position each frame relative to the OrbitedUnit, and provides a Z offset via GetZPos.

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