Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Have a look at our Games Focus blog post series which will show what Unity is doing for all game developers – now, next year, and in the future.
    Dismiss Notice

Question Trying to introduce a flip animation...

Discussion in 'Animation' started by DrSpoutnik, Nov 24, 2022 at 1:07 AM.

  1. DrSpoutnik

    DrSpoutnik

    Joined:
    Jan 14, 2018
    Posts:
    64
    Hi !
    my game is a 2D platformer.
    i have an enemy, a troll, doing a patrol from pointA to pointB.
    i found a nice tutorial to do so, that i customize a bit and added my animation too.
    it works fine, but, i want to have an very short animation for the flipping of the enemy between left and right.
    i have my animation in the animator, with a bool set from "any state" as an entry and both Idle and Walks state.

    but anywhere i put the bool on true for allows the animation to be played, it's not working very well...
    maybe someone could help me to understand the code i'm using to show me where to modify it ?

    here it is :

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class EnemyPatrolZone : MonoBehaviour
    6. {
    7.  
    8.     private Troll_Script TrollScript;     // the Troll as it's own script for setting speed, runspeed etc. This script is attached to a parent empty game object, commanding the patrol zone.
    9.  
    10.     [Header ("Patrol Points")]  // the troll patrol in between
    11.     [SerializeField] private Transform leftEdge;
    12.     [SerializeField] private Transform rightEdge;
    13.  
    14.     [Header("Enemy")]
    15.     [SerializeField] private Transform enemy;  // "enemy" is the troll. i call it "enemy" cause i would reuse it for others kind of enemies...
    16.  
    17.     [Header("Movement parameters")]
    18.  
    19.     private Vector3 initScale;
    20.  
    21.     [Header("Idle Behaviour")]
    22.     [SerializeField] private float idleDuration;
    23.     private float idleTimer; // time to wait before the troll is going back
    24.  
    25.     [Header("Enemy Animator")]
    26.     [SerializeField] private Animator anim;
    27.  
    28.     private void Awake()
    29.     {
    30.         initScale = enemy.localScale;
    31.         TrollScript = gameObject.GetComponentInChildren<Troll_Script>();    
    32.     }
    33.  
    34.     private void Start()
    35.     {
    36.         gameObject.GetComponentInChildren<Animator>().SetBool("Chasing", false);  // a script i use for others actions when enabled....
    37.         gameObject.GetComponentInChildren<Animator>().SetBool("Angry", false); // a script i use for others actions when enabled....
    38.     }
    39.  
    40.  
    41.     private void OnDisable()
    42.     {
    43.         gameObject.GetComponentInChildren<Animator>().SetBool("moving", false);
    44.     }
    45.  
    46.     private void Update()
    47.     {
    48.        
    49.         if (!TrollScript.facingRight)       // this value is set in the troll_script, facingRight= true by default.    
    50.         {
    51.             if (enemy.position.x >= leftEdge.position.x)
    52.                 MoveInDirection(-1);              
    53.             else
    54.                 DirectionChange();              
    55.         }
    56.         else
    57.         {
    58.             if (enemy.position.x <= rightEdge.position.x)
    59.                 MoveInDirection(1);
    60.             else
    61.                 DirectionChange();              
    62.         }
    63.     }
    64.  
    65.     private void DirectionChange()
    66.     {
    67.         gameObject.GetComponentInChildren<Animator>().SetBool("moving", false);
    68.  
    69.         idleTimer += Time.deltaTime;
    70.  
    71.         if (idleTimer > idleDuration)
    72.          
    73.             TrollScript.facingRight = !TrollScript.facingRight;
    74.     }
    75.  
    76.  
    77.  
    78.     private void MoveInDirection(int _direction)
    79.     {
    80.      
    81.         idleTimer = 0;    
    82.         anim.SetBool("moving", true); // "moving" commands the walk animation. here the troll walks again after a while. tried to insert the flip animation here, but then he would flip and move while flipping. i would like him to flip and then move again, in the right direction.
    83.  
    84.         //Make enemy face direction
    85.         enemy.localScale = new Vector3(Mathf.Abs(initScale.x) * _direction,
    86.             initScale.y, initScale.z);
    87.  
    88.         //Move in that direction
    89.         enemy.position = new Vector3(enemy.position.x + Time.deltaTime * _direction * TrollScript.speed,
    90.             enemy.position.y, enemy.position.z);
    91.     }
    92.  
    93. }
    the code i would like to insert is " anim.SetBool("flip", true);" / " anim.SetBool("flip", false)"


    Hope i'm clear, sorry english is not my language ;)
     
unityunity