/// <summary>
/// Component responsible for moving a character on the server side based on inputs.
/// </summary>
[RequireComponent(typeof(NavMeshAgent))]
[RequireComponent(typeof(NetworkCharacterState))]
[RequireComponent(typeof(NetworkCharacterState), typeof(NavMeshAgent))]
private NavMeshAgent navMeshAgent ;
private NetworkCharacterState networkCharacterState ;
private NavMeshAgent m_NavMeshAgent ;
private NetworkCharacterState m_NetworkCharacterState ;
private NavMeshPath path ;
private MovementState movementState ;
private NavMeshPath m_DesiredMovementPath ;
private MovementState m_MovementState ;
private float movementSpeed ; // TODO this should be assigned based on character definition
private float m_MovementSpeed ; // TODO [GOMPS-86] this should be assigned based on character definition
public override void NetworkStart ( )
{
}
// On the server enable navMeshAgent and initialize
n avMeshAgent. enabled = true ;
n etworkCharacterState. OnReceivedClientInput + = SetMovementTarget ;
p ath = new NavMeshPath ( ) ;
m_N avMeshAgent. enabled = true ;
m_N etworkCharacterState. OnReceivedClientInput + = SetMovementTarget ;
m_DesiredMovementP ath = new NavMeshPath ( ) ;
movementState = MovementState . PathFollowing ;
m_M ovementState = MovementState . PathFollowing ;
n avMeshAgent. CalculatePath ( position , p ath) ;
m_N avMeshAgent. CalculatePath ( position , m_DesiredMovementP ath) ;
n avMeshAgent = GetComponent < NavMeshAgent > ( ) ;
n etworkCharacterState = GetComponent < NetworkCharacterState > ( ) ;
m_N avMeshAgent = GetComponent < NavMeshAgent > ( ) ;
m_N etworkCharacterState = GetComponent < NetworkCharacterState > ( ) ;
if ( movementState = = MovementState . PathFollowing )
if ( m_M ovementState = = MovementState . PathFollowing )
n etworkCharacterState. NetworkPosition . Value = transform . position ;
n etworkCharacterState. NetworkRotationY . Value = transform . rotation . eulerAngles . y ;
m_N etworkCharacterState. NetworkPosition . Value = transform . position ;
m_N etworkCharacterState. NetworkRotationY . Value = transform . rotation . eulerAngles . y ;
var corners = p ath. corners ;
var corners = m_DesiredMovementP ath. corners ;
if ( ! corners . Any ( ) )
if ( corners . Length = = 0 )
movementState = MovementState . Idle ;
m_M ovementState = MovementState . Idle ;
var desiredMovementAmount = movementSpeed * Time . fixedDeltaTime ;
var desiredMovementAmount = m_M ovementSpeed * Time . fixedDeltaTime ;
// If there is less distance to move left in the path than our desired amount
if ( Vector3 . SqrMagnitude ( corners [ corners . Length - 1 ] - transform . position ) < ( desiredMovementAmount * desiredMovementAmount ) )
movementState = MovementState . Idle ;
m_MovementState = MovementState . Idle ;
return ;
}
var movementVector = direction * desiredMovementAmount ;
n avMeshAgent. Move ( movementVector ) ;
m_N avMeshAgent. Move ( movementVector ) ;
n avMeshAgent. CalculatePath ( corners [ corners . Length - 1 ] , p ath) ;
m_N avMeshAgent. CalculatePath ( corners [ corners . Length - 1 ] , m_DesiredMovementP ath) ;
}
}
}