If I’m understanding your example correctly, PlayerMover is implementing the IMove interface, which means that it is implementing the property float Speed {get {return speed;}};
PlayerMover sets the speed (presumably based on input), and now property Speed is available for anybody looking at PlayerMover.
public class PlayerMover: IMove
{
float speed;
public float Speed {get => speed;}
void Update
{
speed = whatever calculations you need to make to get the speed
}
}
CharacterAnimation is getting the value from PlayerMover, so not implementing the interface, but using the interface to get the information…
IMove mover;
void Awake
{
IMove mover = GetComponent<IMove>();
}
void Update
{
Set animation based on mover.Speed;
}
So the player would be set up with the PlayerMover and the CharacterAnimation components…
Your question is about a second class implementing the IMove interface, so let’s look at doing just that. We’re going to create a new class EnemyMover
public class EnemyMover: IMover
{
float speed;
public float Speed {get => speed;}
void Update
{
speed = ai based calculations to derive speed
}
}
Now this class, along with your CharacterAnimation class would go on the enemy.
Note that the CharacterAnimation class requires no changes, because EnemyMover is also an IMove, as far as that class is concerned there is no difference between a PlayerMover and an EnemyMover… it finds the EnemyMover in Awake, and gets the speed in Update to do its magic to animate the character.
The interface itself does not hold the reference to Speed. Each IMove based component is solely responsible for providing that value. IMove is just a way of communicating between components. Since all we’re interested in from the IMove components is the Speed, CharacterAnimation doesn’t have to care “was this a player? Was it an enemy?” The actual setting is in PlayerMover or EnemyMover, not in the interface itself.
Without the interface, CharacterAnimation would have to look like this:
public class CharacterAnimation
{
PlayerMover player;
EnemyMover enemy;
void Awake()
{
player = GetComponent<PlayerMover>();
enemy = GetComponent<EnemyMover>();
}
void Update
{
float speed = 0;
if(player) speed = player.Speed
else if(enemy) speed = enemy.Speed;
do all my fun logic with speed;
}
}
While this doesn’t sound too horrible, this is just a small example… Interfaces allow us to have many different classes, all unrelated to one another except that they expose the required properties or methods in the interface. How those properties and methods are implemented is up to each class as it goes along, but any class that calls on that interface can be assured those methods will exist.