Stop the game object without stopping its Animation (Mecanim)

Hi!

I have animation that moves the object in space. Such a simple case, the character (box collider, isKinematic = true), runs forward (animation changes its position.) On his way there is an enemy (box collider, isKinematic = true), it is necessary so that the character could not run through it. In this case, it is necessary that when a character approached the enemy close, it does not move (not to go through), but the animation did not stop.

I’m doing a check in LateUpdate the intersection Bounds of Colliders and RigidBody SweepTest, if there is hit or intersection, then make _animator.applyRootMotion = false. This stops a character on the spot, while playing the animation. But, if the animation contains such bounce back after a run, then I have to turn back mode _animator.applyRootMotion for resume movement of the character.

Animation looks like - a fighter running away, does a kick, and runs back.
In my case it would be something like:

  1. Fighter running
  2. Check SweepTest Hit or Bounds Intersection // during the animation soldier approached the enemy
  3. applyRootMotion = false, still run frame or frame kick // animation is played, and the character is in place before the enemy
  4. Now in animation fighter should run back ago, but since it is one big animation, it is not clear how to turn when you need applyRootMotion = true;

That is, it is necessary that all forward displacement of the object animation stopped transforms shift before enemy (applyRootMotion = false), but did not stop the animation. And all the backward movement of the object happening with animation (applyRootMotion = true).

Or what that is another solution?

I use it for fighting game.
May be easier and better to do manual control (via script - transform.Translate(…)) a fighter and not through animation?

Thanks!

The animation looks so:
1221101--50351--$capture.gif

Code for distance check and change position:

void OnAnimatorMove()
	{
		Bounds hitBoxBounds = _hitBox.collider.bounds;
		Bounds enemyHitBoxBounds = _enemyFighter.HitBox.collider.bounds;
		
		bool boundsIntersects = hitBoxBounds.Intersects(enemyHitBoxBounds);
		bool bodySweepHit = false;
		
		if (!boundsIntersects)
		{
			Rigidbody hitBoxRigidbody = _hitBox.rigidbody;
			Rigidbody enemyHitBoxRigidbody = _enemyFighter.HitBox.rigidbody;
			
			RaycastHit hit;
			bodySweepHit = hitBoxRigidbody.SweepTest(_hitBox.transform.forward, out hit, MIN_FIGHT_DISTANCE);
		}
		
		bool fightDistanceOver = boundsIntersects || bodySweepHit;
		
		if (!fightDistanceOver)
		{
			transform.position = _animator.rootPosition;
		} else { ?? }
	}

May be used Curves to determine which side is currently moving fighter in the animation, and if he moves back, allow “transform.position = _animator.rootPosition”, and if moving forward, then check for collision and prohibit displacement.

May be possible to somehow adapt CharacterController for fighting game?

Thanks!