unity navmesh jump code example
Example: for navmesh jump component used in unity
//-----------------------------------------------------------------------------------
// This will cause the Crawler to disappear briefly the first time it is called, due
// to NavMeshAgent.Stop( true ) being called. TODO: Find out why this happens.
private IEnumerator Attacking()
{
// Stop or disable the navagent before you apply the force, otherwise it will
// stick to the navmesh.
_navMeshAgent.Stop( true );
BlendOutCurrentAnimation();
BlendInAnimation( AnimationType.Attack, 0.1f );
// Apply a force to make the Crawler jump
_rigidbody.is$$anonymous$$inematic = false;
_rigidbody.useGravity = true;
_rigidbody.AddForce( ComputeJumpForce(), ForceMode.Impulse );
yield return new WaitForSeconds( JUMP_DURATION * JUMP_HIT_TEST_PLAYER_FACTOR );
if( PlayerIsCloseEnoughToHit() )
{
HudManager.Instance.DisplayHitFlash();
// TODO: Spawn a 3rd person crawler?
_state = CrawlerState.FaceHuggingPlayer;
}
else
{
// We missed so fall to the floor
// A "bounce" animation that should be played once the crawler/jumper
// hits the floor (so it should look like they 'bounce' back into their standing position.
_state = CrawlerState.Bouncing;
}
yield return new WaitForSeconds( JUMP_DURATION * (1f - JUMP_HIT_TEST_PLAYER_FACTOR) );
// Turn the NavMeshAgent back on
_navMeshAgent.Resume();
// Turn the rigid body physics back to how they were before.
_rigidbody.is$$anonymous$$inematic = true;
_rigidbody.useGravity = false;
yield return null;
}
//-----------------------------------------------------------------------------------
private void Start()
{
_target = GameObject.FindGameObjectWithTag( "Player" );
SetupNavMeshAgent();
SetupAnimations();
AddRigidBody();
}
//-----------------------------------------------------------------------------------
private void SetupNavMeshAgent()
{
_navMeshAgent = GetComponent<NavMeshAgent>();
if (_navMeshAgent != null)
{
_navMeshAgent.enabled = false;
Debug.Log("Crawler: NavMeshAgent found");
}
else
{
Debug.LogError("Crawler: NavMeshAgent not found");
}
}
//-----------------------------------------------------------------------------------
private void AddRigidBody()
{
_rigidbody = gameObject.AddComponent( "Rigidbody" ) as Rigidbody;
if( _rigidbody != null )
{
_rigidbody.mass = MASS;
Debug.Log( "Crawler: Rigidbody added" );
}
else
{
Debug.LogError( "Crawler: Rigidbody not added" );
}
}