Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice
  3. Dismiss Notice

Question 2D Animation Setting Every Frame

Discussion in 'Scripting' started by jlpeyton, May 22, 2024.

  1. jlpeyton

    jlpeyton

    Joined:
    Dec 22, 2017
    Posts:
    64
    When the player is not in attack range and the NPC goes into Suspicion it should idle for a few seconds and then resume patrolling. Instead the animation keeps repeating and then it goes back to patrolling with this repeating animation.

    Here's a recording of what's occuring:
    https://drive.google.com/file/d/1ExG_CH3IPnO0YyAyI0oTxtR_eVDRl58q/view?usp=sharing

    Code (CSharp):
    1. public class AIController : MonoBehaviour
    2. {
    3.     [SerializeField] float suspicionTime = 3f;
    4.  
    5.     float timeSinceLastSawPlayer = Mathf.Infinity;
    6.  
    7.     private void Update()
    8.     {
    9.         if (health.IsDead()) { return; }
    10.  
    11.         if (InAttackRangeOfPlayer() && fighter.CanAttack(player))
    12.         {
    13.             AttackBehavior();
    14.         }
    15.         else if (timeSinceLastSawPlayer < suspicionTime)
    16.         {
    17.             SuspicionBehavior();
    18.         }
    19.         else
    20.         {
    21.             PatrolBehavior();
    22.  
    23.         }
    24.  
    25.         UpdateTimers();
    26.  
    27.     }
    28.  
    29.     private void PatrolBehavior()
    30.     {
    31.         Vector3 nextPosition = guardPosition;
    32.  
    33.         if (patrolPath != null)
    34.         {
    35.             if (AtWaypoint())
    36.             {
    37.                 timeSinceArriveAtWaypoint = 0;
    38.                 CycleWaypoint();
    39.             }
    40.             nextPosition = GetCurrentWaypoint();
    41.         }
    42.  
    43.         if (timeSinceArriveAtWaypoint > waypointDwellTime)
    44.         {
    45.             mover.StartMoveAction(nextPosition, patrolSpeedFraction);
    46.         }
    47.         else
    48.         {
    49.             mover.Cancel();
    50.         }
    51.  
    52.     }
    53.  
    54.     private void SuspicionBehavior()
    55.     {
    56.         mover.Cancel();
    57.     }
    58.  
    59. }
    Code (CSharp):
    1. public class Mover : MonoBehaviour, IAction
    2.     {
    3.         private Direction lastMovementDirection;
    4.         private Vector2 lastDirection;
    5.         public bool isMovingAnimationSet = false;
    6.         [HideInInspector]
    7.         public bool isIdling = true;
    8.  
    9.         private enum Direction
    10.         {
    11.             Left,
    12.             Right,
    13.             Up,
    14.             Down,
    15.             None
    16.         }
    17.  
    18.         private void Update()
    19.         {
    20.             bool isDead = health.IsDead();
    21.             rb.isKinematic = isDead;
    22.             capsuleCollider.enabled = !isDead;
    23.             Debug.Log(isIdling);
    24.         }
    25.  
    26.         private void FixedUpdate()
    27.         {
    28.             if (rb != null)
    29.             {
    30.                 rb.velocity = velocity;
    31.             }
    32.         }
    33.  
    34.         public void StartMoveAction(Vector2 destination, float speedFraction)
    35.         {
    36.             if (!isMovingAnimationSet)
    37.             {
    38.                 Debug.Log("Start Moving");
    39.                 isIdling = false;
    40.                 isMovingAnimationSet = true;
    41.                 ChangeAnimation("Walk", GetLastDirection());
    42.             }
    43.  
    44.             MoveTo(destination, speedFraction);
    45.         }
    46.  
    47.         public void MoveTo(Vector2 destination, float speedFraction)
    48.         {
    49.             Vector2 currentPosition = transform.position;
    50.             Vector2 direction = (destination - currentPosition).normalized;
    51.             velocity = direction * maxSpeed * Mathf.Clamp01(speedFraction);
    52.  
    53.             Direction currentMovementDirection = GetMovementDirection(direction);
    54.  
    55.             if (currentMovementDirection != lastMovementDirection)
    56.             {
    57.                 lastMovementDirection = currentMovementDirection;
    58.                 lastDirection = direction;
    59.                 Debug.Log("Moving To");
    60.                 ChangeAnimation("Walk", direction);
    61.             }
    62.         }
    63.  
    64.         public void Cancel()
    65.         {
    66.             velocity = Vector2.zero;
    67.  
    68.             if (!isIdling)
    69.             {
    70.                 isIdling = true;
    71.                 isMovingAnimationSet = false;
    72.                 ChangeAnimation("Idle", GetLastDirection());
    73.             }
    74.         }
    75.  
    76.         public Vector2 GetLastDirection()
    77.         {
    78.             return lastDirection;
    79.         }
    80.  
    81.         public bool IsMoving
    82.         {
    83.             get { return velocity != Vector2.zero; }
    84.         }
    85.  
    86.     }
     
    Last edited: May 22, 2024
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    39,357
    Sounds like a bug! Could be in the animation setup so always start there. If the animation does not respond to properties correctly, all the code in the world won't fix it.

    Once you're convinced the underlying animation is set up properly, then move onto debugging the code itself.

    By debugging you can find out exactly what your program is doing so you can fix it.

    https://docs.unity3d.com/Manual/ManagedCodeDebugging.html

    Use the above techniques to get the information you need in order to reason about what the problem is.

    You can also use
    Debug.Log(...);
    statements to find out if any of your code is even running. Don't assume it is.

    Once you understand what the problem is, you may begin to reason about a solution to the problem.
     
  3. jlpeyton

    jlpeyton

    Joined:
    Dec 22, 2017
    Posts:
    64
    It's not the animation I've confirmed that step. It's how the code is executing the animation. I've debugged for hours trying to figure out where this is messing up including setting breakpoints but I've yet to be able to pinpoint the exact issue. I'll probably start stripping away logic and slowing introduce things back in.
     
  4. dstears

    dstears

    Joined:
    Sep 6, 2021
    Posts:
    216
    Can you share the ChangeAnimation function?

    It would probably help to put a Debug.Log statement in the ChangeAnimation function or just before every place this function is called to see if it is getting called at a time you do not expect. Perhaps something is calling this every frame and is causing the walk animation to restart constantly.
     
    Kurt-Dekker likes this.