How to detect distance from Character?

There are 2 gameobjects:

  1. Character (C)
  2. Opponent (O)

in a 2D fighting game.

How does Opponent detect the distance of Character?

(O) ----------------- x -----------------(C)

How to get the x value dynamically?

(O) and (C) are keep moving.

You can use Vector3.Distance:

var distance = Vector3.Distance(character.position, opponent.position);