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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Question How to go to the next round

Discussion in 'Scripting' started by CaptainVega, Jan 27, 2023.

  1. dlorre

    dlorre

    Joined:
    Apr 12, 2020
    Posts:
    700
    What you can do is invert the conditions, to set the most restrictive first, so here swap the two codes. Another way would be to change the first condition by checking if ((player1.roundWin + player2.roundWin) == 1).
     
  2. CaptainVega

    CaptainVega

    Joined:
    Jan 26, 2023
    Posts:
    102
    @dlorre So?
    Code (CSharp):
    1. void NextRoundOfWin()
    2.     {
    3.         battleController.NextRound();
    4.  
    5.         if ((player1.roundWin + player2.roundWin) == 1)
    6.         {
    7.             showRound2Fight();
    8.          
    9.         }
    10.         else if(player1.roundWin >= 1 || player2.roundWin >= 1)
    11.         {
    12.             showFinalRoundFight();
    13.         }
    14.      
    15.     }
    (That one works but I don't like the bug)
     
    Last edited: Feb 1, 2023
  3. CaptainVega

    CaptainVega

    Joined:
    Jan 26, 2023
    Posts:
    102
    because I don't like these bugs, it's because when I want to make ryu win 3 rounds I have to reverse the conditions and that's a mess (and confusing:confused:), and I also tried to put the first one in the condition but it didn't work
    Code (CSharp):
    1.  void NextRoundOfWin()
    2.     {
    3.         battleController.NextRound();
    4.  
    5.         if ((player1.roundWin + player2.roundWin) == 1)
    6.         {
    7.             showFinalRoundFight();
    8.         }
    9.         else if(player1.roundWin == 1 || player2.roundWin == 1)
    10.         {
    11.             showRound2Fight();
    12.         }
    13.    
    14.  
    15.     }
    or so
    Code (CSharp):
    1.  void NextRoundOfWin()
    2.     {
    3.         battleController.NextRound();
    4.  
    5.         if ((player1.roundWin + player2.roundWin) == 1)
    6.         {
    7.             showFinalRoundFight();
    8.         }
    9.          if (player1.roundWin == 1 || player2.roundWin == 1)
    10.         {
    11.             showRound2Fight();
    12.         }
    13.    
    14.  
    15.     }
    Is there a solution to that that is not a bug? oh, and it also activates two animations for no reason.
     
    Last edited: Feb 1, 2023
  4. dlorre

    dlorre

    Joined:
    Apr 12, 2020
    Posts:
    700
    You had this code:

    Code (csharp):
    1.  
    2.         if(player1.roundWin >= 1 || player2.roundWin >= 1)
    3.         {
    4.             showRound2Fight();
    5.         }
    6.         else if (player1.roundWin == 1 && player2.roundWin == 1)
    7.         {
    8.             showFinalRoundFight();
    9.             Debug.Log("hey Unity is working Round Final animation??");
    10.         }
    11.  
    But the first condition is less restrictive than the second so it matches always and you never got into the second one, so you would put the second one first:

    Code (csharp):
    1.  
    2.         if (player1.roundWin == 1 && player2.roundWin == 1)
    3.         {
    4.             showFinalRoundFight();
    5.             Debug.Log("hey Unity is working Round Final animation??");
    6.         }      
    7.  
    8.       else if(player1.roundWin >= 1 || player2.roundWin >= 1)
    9.         {
    10.             showRound2Fight();
    11.        }
    12.  
     
    CaptainVega likes this.
  5. CaptainVega

    CaptainVega

    Joined:
    Jan 26, 2023
    Posts:
    102
    Congratulations:);). We have finished Next Round:D, Thank you very much @dlorre. If I need a question, I'll let you know, yeso_O;)? If you'll excuse me @dlorre, I'm going to fix some bugs in my project. Thank you @dlorre :):D;):rolleyes:.
     
  6. CaptainVega

    CaptainVega

    Joined:
    Jan 26, 2023
    Posts:
    102
    Hello, it's just one last question:D. when i was fixing the bugs, I tried to make it stay in idle but I couldn't do it:(.
    Code (CSharp):
    1.  void Update()
    2.     {
    3.         animator.SetFloat("health", healtPercent);
    4.  
    5.         if (oponent != null)
    6.         {
    7.             animator.SetFloat("oponent_health", oponent.healtPercent);
    8.  
    9.  
    10.         }
    11.         else
    12.         {
    13.             animator.SetFloat("oponent_health", 1);
    14.          
    15.         }
    16.  
    17.         if (enable)
    18.         {
    19.             if (player == PlayerType.HUMAN)
    20.             {
    21.                 UpdateHumanInput();
    22.                 FighterStates currentState = FighterStates.IDLE;
    23.             }
    24.          
    25.             else
    26.             {
    27.                 UpdateAiInput();
    28.                 FighterStates currentState = FighterStates.IDLE;
    29.             }
    30.  
    31.         }
    32.  
    33.         if (healt <= 0 && currentState != FighterStates.DEAD)
    34.         {
    35.             animator.SetTrigger("DEAD");
    36.  
    37.         }
    38.        
    39.     }
    40.  
    41.     private float getDistanceToOponennt()
    42.     {
    43.         return Mathf.Abs(transform.position.x - oponent.transform.position.x);
    44.  
    45.     }
    46.  
    47.     public virtual void hurt(float damage)
    48.     {
    49.         if (!invulnerable)
    50.         {
    51.             if (defending)
    52.             {
    53.                 damage *= 0.2f;
    54.             }
    55.             if (healt >= damage)
    56.             {
    57.                 healt -= damage;
    58.             }
    59.             else
    60.             {
    61.                 healt = 0;
    62.             }
    63.  
    64.             if (healt > 0)
    65.             {
    66.                 animator.SetTrigger("TAKE_HIT");
    67.             }
    68.  
    69.  
    70.  
    71.  
    72.         }
    73.     }
    74.     public virtual void hurt2(float damage)
    75.     {
    76.         if (!invulnerable)
    77.         {
    78.  
    79.             if (healt >= damage)
    80.             {
    81.                 healt -= damage;
    82.             }
    83.             else
    84.             {
    85.                 healt = 0;
    86.             }
    87.  
    88.             if (healt > 0)
    89.             {
    90.                 animator.SetTrigger("TAKE_HITVolar");
    91.             }
    92.         }
    93.     }
    94.  
    95.  
    96.  
    97.     public void playSound(AudioClip sound)
    98.     {
    99.         GameUtils.playSound(sound, audioPlayer);
    100.     }
    101.  
    102.     public bool invulnerable
    103.     {
    104.         get
    105.         {
    106.             return currentState == FighterStates.TAKE_HIT
    107.                 || currentState == FighterStates.TAKE_HIT_DEFEND
    108.                 || currentState == FighterStates.TAKE_HITVolar
    109.                     || currentState == FighterStates.DEAD;
    110.         }
    111.     }
    112.  
    113.     public bool defending
    114.     {
    115.         get
    116.         {
    117.             return currentState == FighterStates.DEFEND
    118.                 || currentState == FighterStates.TAKE_HIT_DEFEND;
    119.         }
    120.     }
    121.  
    122.     public bool attacking
    123.     {
    124.         get
    125.         {
    126.             return currentState == FighterStates.ATTACK;
    127.  
    128.         }
    129.     }
    130.    
    131.     public float healtPercent
    132.     {
    133.         get
    134.         {
    135.             return healt / MAX_HEALTH;
    136.         }
    137.     }
    138.  
    139.     public Rigidbody body
    140.     {
    141.         get
    142.         {
    143.             return this.myBody;
    144.         }
    145.     }
    146. }
    When the "enable" is deactivated, I press the left arrow or the letter A, it will keep moving until the "enable" is activated, it stops pressing.



    When I finish fixing some bugs I'll show you my result;).
     
  7. dlorre

    dlorre

    Joined:
    Apr 12, 2020
    Posts:
    700
    What is it that makes him idle, is it FighterStates, or a parameter in your animator?
     
  8. CaptainVega

    CaptainVega

    Joined:
    Jan 26, 2023
    Posts:
    102
    I don't mean that, I mean that if I press to go left before the "enable" is deactivated (after the round wins) then I stop pressing left and my fighter doesn't stop going left if I'm not. pressing, until "enable" is activated that is bad when the next round begins when the position is reset.

    I will show you again the video with more detail than what I mean by this, showing with the keyboard on the screen

    Ken is also included, that's why he hits or walks without waiting for him to say FIGHT! when you reset the position.
    and how do you see the previous videos that I published.
     
  9. CaptainVega

    CaptainVega

    Joined:
    Jan 26, 2023
    Posts:
    102
    what i want to do is that when i hit the keyboard go back to my fighter before the "enable" is deactivated, it automatically goes back to idle when the "enable" is deactivated and does not continue walking back until I say FIGHT! and also to Ken (that he no longer hit him and walk towards him Ryu).

    That's why Ryu is mad at Ken, because Ken didn't listen to the combat rule, and he doesn't want to fight Ken anymore:D:D.
     
  10. dlorre

    dlorre

    Joined:
    Apr 12, 2020
    Posts:
    700
    OK, so what do you need? To know how to detect a keyboard press or how to make him idle?
     
  11. CaptainVega

    CaptainVega

    Joined:
    Jan 26, 2023
    Posts:
    102
    Like fighting games, when the round is over, it stops moving to return to idle, an example

    the opponent does not wait for the order to start the round, he hits without stopping, my character Ryu also waits for the order to start, that is, idle.

    so that means "how to make it idle" after the idle round ends (As I told you before if the "enable" is deactivated I automatically went to idle until the "enable" is activated now if you can move and attack the opponent.)
     
    Last edited: Feb 3, 2023
  12. dlorre

    dlorre

    Joined:
    Apr 12, 2020
    Posts:
    700
    Well, I am asking you what line of code do you write to make it idle? Once we figure that out we can arrange something to do what you expect.
     
  13. CaptainVega

    CaptainVega

    Joined:
    Jan 26, 2023
    Posts:
    102
    OK.
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5. public class Fighter : MonoBehaviour
    6. {
    7.     public enum PlayerType
    8.     {
    9.         HUMAN, AI
    10.     };
    11.     public static float MAX_HEALTH = 100f;
    12.     public BannerController banner;
    13.     public float healt = MAX_HEALTH;
    14.     public string fighterName;
    15.     public Fighter oponent;
    16.     public bool enable;
    17.      float speed = 0.0f;
    18.      float acceleration = 0.1f;
    19.      float deceleration = 0.5f;
    20.     public PlayerType player;
    21.    
    22.     public Transform luchador;
    23.  
    24.     public FighterStates currentState = FighterStates.IDLE;
    25.  
    26.     protected Animator animator;
    27.     private Rigidbody myBody;
    28.     private AudioSource audioPlayer;
    29.  
    30.     public BattleController battleController;
    31.  
    32.  
    33.  
    34.     //for AI only
    35.     private float random;
    36.     private float randomSetTime;
    37.  
    38.     public Image roundOne;
    39.  
    40.     public Image primerRound;
    41.    
    42.     // ResetStats
    43.     public Vector3 StartPoint;
    44.  
    45.     // FighterStates currentState = FighterStates.IDLE;
    46.     public void ResetStats()
    47.     {
    48.         transform.position = StartPoint;
    49.         FighterStates currentState = FighterStates.IDLE;
    50.         healt = MAX_HEALTH;
    51.         enable = false;
    52.         animator.SetFloat("health", 1);
    53.         animator.SetFloat("opponent_health", 1);
    54.        
    55.     }
    56.  
    57.     public int roundWin;
    58.     public Text scoreTextRound;
    59.     public void addScoreRound()
    60.     {
    61.         roundWin = roundWin + 1;
    62.         scoreTextRound.text = roundWin.ToString();
    63.     }
    64.     public void CheckVictory()
    65.     {
    66.         if (roundWin >= 2)
    67.         {
    68.             Debug.Log("Win Ryu");
    69.             primerRound.enabled = true;
    70.             battleController.player1.roundOne.enabled = false;
    71.             battleController.player2.roundOne.enabled = false;
    72.          
    73.  
    74.         }
    75.     }
    76.  
    77.     // Use this for initialization
    78.     void Start()
    79.     {
    80.         myBody = GetComponent<Rigidbody>();
    81.         animator = GetComponent<Animator>();
    82.         audioPlayer = GetComponent<AudioSource>();
    83.         StartPoint = transform.position;
    84.      
    85.     }
    86.  
    87.     public void UpdateHumanInput()
    88.     {
    89.        
    90.             bool moverseoprimido = Input.GetKey(KeyCode.RightArrow);
    91.  
    92.  
    93.  
    94.         if (moverseoprimido && speed < 1.0f)
    95.         {
    96.             speed += Time.deltaTime * acceleration;
    97.         }
    98.         if (!moverseoprimido && speed > 0.0f)
    99.         {
    100.             speed -= Time.deltaTime * deceleration;
    101.         }
    102.         if (!moverseoprimido && speed < 0.0f)
    103.         {
    104.             speed = 0.0f;
    105.         }
    106.         if (Input.GetAxis("Horizontal") < -0.1)
    107.         {
    108.  
    109.             if (oponent.attacking)
    110.             {
    111.                 animator.SetBool("WALK_BACK", false);
    112.                 animator.SetBool("DEFEND", true);
    113.             }
    114.             else
    115.             {
    116.                 animator.SetBool("WALK_BACK", true);
    117.                 animator.SetBool("DEFEND", false);
    118.             }
    119.         }
    120.         else
    121.         {
    122.             animator.SetBool("WALK_BACK", false);
    123.             animator.SetBool("DEFEND", false);
    124.         }
    125.  
    126.         if (Input.GetAxis("Vertical") < -0.1)
    127.         {
    128.             animator.SetBool("DUCK", true);
    129.         }
    130.         else
    131.         {
    132.             animator.SetBool("DUCK", false);
    133.         }
    134.  
    135.         if (Input.GetKeyDown(KeyCode.UpArrow))
    136.         {
    137.             animator.SetTrigger("JUMP");
    138.         }
    139.  
    140.         if (Input.GetKeyDown(KeyCode.Space))
    141.         {
    142.             animator.SetTrigger("PUNCH_R");
    143.         }
    144.  
    145.         if (Input.GetKeyDown(KeyCode.K))
    146.         {
    147.             animator.SetTrigger("KICK_R");
    148.         }
    149.  
    150.         if (Input.GetKeyDown(KeyCode.H))
    151.         {
    152.             animator.SetTrigger("HADOKEN");
    153.         }
    154.  
    155.  
    156.         animator.SetFloat("Walk", speed);
    157.        
    158.     }
    159.  
    160.  
    161.     public void UpdateAiInput()
    162.     {
    163.         animator.SetBool("defending", defending);
    164.         //animator.SetBool ("invulnerable", invulnerable);
    165.         //animator.SetBool ("enable", enable);
    166.  
    167.         animator.SetBool("oponent_attacking", oponent.attacking);
    168.         animator.SetFloat("distanceToOponent", getDistanceToOponennt());
    169.  
    170.         if (Time.time - randomSetTime > 1)
    171.         {
    172.             random = Random.value;
    173.             randomSetTime = Time.time;
    174.         }
    175.         animator.SetFloat("random", random);
    176.     }
    177.  
    178.     // Update is called once per frame
    179.     void Update()
    180.     {
    181.         animator.SetFloat("health", healtPercent);
    182.  
    183.         if (oponent != null)
    184.         {
    185.             animator.SetFloat("oponent_health", oponent.healtPercent);
    186.  
    187.  
    188.         }
    189.         else
    190.         {
    191.             animator.SetFloat("oponent_health", 1);
    192.          
    193.         }
    194.  
    195.         if (enable)
    196.         {
    197.             // FighterStates currentState = FighterStates.IDLE;
    198.             if (player == PlayerType.HUMAN)
    199.             {
    200.                 UpdateHumanInput();
    201.                 FighterStates currentState = FighterStates.IDLE;
    202.             }
    203.          
    204.             else
    205.             {
    206.                 UpdateAiInput();
    207.                 FighterStates currentState = FighterStates.IDLE;
    208.             }
    209.            
    210.  
    211.         }
    212.  
    213.         if (healt <= 0 && currentState != FighterStates.DEAD)
    214.         {
    215.             animator.SetTrigger("DEAD");
    216.  
    217.         }
    218.        
    219.     }
    220.  
    221.     private float getDistanceToOponennt()
    222.     {
    223.         return Mathf.Abs(transform.position.x - oponent.transform.position.x);
    224.  
    225.     }
    226.  
    227.     public virtual void hurt(float damage)
    228.     {
    229.         if (!invulnerable)
    230.         {
    231.             if (defending)
    232.             {
    233.                 damage *= 0.2f;
    234.             }
    235.             if (healt >= damage)
    236.             {
    237.                 healt -= damage;
    238.             }
    239.             else
    240.             {
    241.                 healt = 0;
    242.             }
    243.  
    244.             if (healt > 0)
    245.             {
    246.                 animator.SetTrigger("TAKE_HIT");
    247.             }
    248.  
    249.  
    250.  
    251.  
    252.         }
    253.     }
    254.     public virtual void hurt2(float damage)
    255.     {
    256.         if (!invulnerable)
    257.         {
    258.  
    259.             if (healt >= damage)
    260.             {
    261.                 healt -= damage;
    262.             }
    263.             else
    264.             {
    265.                 healt = 0;
    266.             }
    267.  
    268.             if (healt > 0)
    269.             {
    270.                 animator.SetTrigger("TAKE_HITUP");
    271.             }
    272.         }
    273.     }
    274.  
    275.  
    276.  
    277.     public void playSound(AudioClip sound)
    278.     {
    279.         GameUtils.playSound(sound, audioPlayer);
    280.     }
    281.  
    282.     public bool invulnerable
    283.     {
    284.         get
    285.         {
    286.             return currentState == FighterStates.TAKE_HIT
    287.                 || currentState == FighterStates.TAKE_HIT_DEFEND
    288.                 || currentState == FighterStates.TAKE_HITVolar
    289.                     || currentState == FighterStates.DEAD;
    290.         }
    291.     }
    292.  
    293.     public bool defending
    294.     {
    295.         get
    296.         {
    297.             return currentState == FighterStates.DEFEND
    298.                 || currentState == FighterStates.TAKE_HIT_DEFEND;
    299.         }
    300.     }
    301.  
    302.     public bool attacking
    303.     {
    304.         get
    305.         {
    306.             return currentState == FighterStates.ATTACK;
    307.  
    308.         }
    309.     }
    310.     public bool attackingVolar
    311.     {
    312.         get
    313.         {
    314.             return currentState == FighterStates.ATTACKVolar;
    315.         }
    316.     }
    317.     public float healtPercent
    318.     {
    319.         get
    320.         {
    321.             return healt / MAX_HEALTH;
    322.         }
    323.     }
    324.  
    325.     public Rigidbody body
    326.     {
    327.         get
    328.         {
    329.             return this.myBody;
    330.         }
    331.     }
    332. }
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5. using UnityEngine.SceneManagement;
    6.  
    7. public class BattleController : MonoBehaviour
    8. {
    9.     public int roundTime = 100;
    10.     private float lastTimeUpdate = 0;
    11.     private bool battleStarted;
    12.     private bool battleEnded;
    13.     public Image ryu;
    14.     public Fighter player1;
    15.     public Fighter player2;
    16.     public BannerController banner;
    17.     public AudioSource musicPlayer;
    18.     public AudioClip backgroundMusic;
    19.     private RoundTracker roundTracker;
    20.     public HudController hudController;
    21.  
    22.  
    23.     void Start()
    24.     {
    25.     banner.showRoundFight();
    26.     ryu.enabled = false;
    27.        
    28.     }
    29.     // FighterStates currentState = FighterStates.IDLE;
    30.     public void NextRound()
    31.     {
    32.         roundTime = 100;
    33.         player2.ResetStats();
    34.         player1.ResetStats();
    35.         hudController.leftBar.size += 1f;
    36.         hudController.rightBar.size += 1f;
    37.         battleEnded = false;
    38.         battleStarted = false;
    39.         FighterStates currentState = FighterStates.IDLE;
    40.     }
    41.  
    42.    
    43.  
    44.  
    45.     private void expireTime()
    46.     {
    47.  
    48.         if (player1.healtPercent > player2.healtPercent)
    49.         {
    50.             player2.healt = 0;
    51.            
    52.          
    53.         }
    54.         else
    55.         {
    56.             player1.healt = 0;
    57.            
    58.          
    59.         }
    60.     }
    61.  
    62.  
    63.  
    64.     void Update()
    65.     {
    66.         if (!battleStarted && !banner.isAnimating)
    67.         {
    68.             battleStarted = true;
    69.  
    70.             player1.enable = true;
    71.             player2.enable = true;
    72.         }
    73.  
    74.         if (battleStarted && !battleEnded)
    75.         {
    76.             if (roundTime > 0 && Time.time - lastTimeUpdate > 1)
    77.             {
    78.                 roundTime--;
    79.                 lastTimeUpdate = Time.time;
    80.                 if (roundTime == 0)
    81.                 {
    82.                     expireTime();
    83.                 }
    84.             }
    85.  
    86.             if (player1.healtPercent <= 0)
    87.             {
    88.                 banner.showYouLose();
    89.                 player2.roundOne.enabled = true;
    90.                 battleEnded = true;
    91.                 player2.addScoreRound();
    92.                 player2.CheckVictory();
    93.             }
    94.             else if (player2.healtPercent <= 0)
    95.             {
    96.                 banner.showYouWin();
    97.                 player1.roundOne.enabled = true;
    98.                 battleEnded = true;
    99.                 player1.addScoreRound();
    100.                 player1.CheckVictory();
    101.             }
    102.         }
    103.     }
    104.  
    105.  
    106. }
    107.  
    108. not sure if it has to do with the FighterStageBehavior Script
    109.  
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class FighterStageBehavior : StateMachineBehaviour
    6. {
    7.     public FighterStates behaviorState;
    8.     public AudioClip soundEffect;
    9.  
    10.     public float horizontalForce;
    11.     public float verticalForce;
    12.  
    13.     protected Fighter fighter;
    14.  
    15.  
    16.  
    17.  
    18.     override public void OnStateEnter(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
    19.     {
    20.         if (fighter == null)
    21.         {
    22.             fighter = animator.gameObject.GetComponent<Fighter>();
    23.         }
    24.  
    25.         fighter.currentState = behaviorState;
    26.  
    27.         if (soundEffect != null)
    28.         {
    29.             fighter.playSound(soundEffect);
    30.         }
    31.  
    32.         fighter.body.AddRelativeForce(new Vector3(0, verticalForce, 0) * Time.deltaTime);
    33.  
    34.  
    35.     }
    36.  
    37.     override public void OnStateUpdate(Animator animator,
    38.                                        AnimatorStateInfo stateInfo, int layerIndex)
    39.     {
    40.         fighter.body.AddRelativeForce(new Vector3(0, 0, horizontalForce));
    41.     }
    42. }
     
  14. dlorre

    dlorre

    Joined:
    Apr 12, 2020
    Posts:
    700
    Ok, what is FighterStates exactly? Click on it in VisualStudio and press F12.
     
  15. CaptainVega

    CaptainVega

    Joined:
    Jan 26, 2023
    Posts:
    102
    that contains.
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public enum FighterStates
    6. {
    7.     IDLE, WALK, WALK_BACK, JUMP, DUCK, HADOKEN,
    8.     ATTACK, ATTACKVolar, TAKE_HIT, TAKE_HITVolar, TAKE_HIT_DEFEND,
    9.     DEFEND, CELEBRATE, DEAD, NONE
    10. }
    11.  
     
  16. dlorre

    dlorre

    Joined:
    Apr 12, 2020
    Posts:
    700
    Ok, so that does nothing it's an enum that is used to keep track of your state. If it was a property we might have found stuff in there.

    I'm not very familiar with state machines in Unity but that's what you are using. You can find the docs here:

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

    What I suggest is that you look at your animator to find how to get your player idle and once you have the info you can translate it in code. Probably animator.SetTrigger("IDLE") should be enough if you find a trigger with this name in your animator. If not then you should make one and use it.
     
    CaptainVega likes this.
  17. CaptainVega

    CaptainVega

    Joined:
    Jan 26, 2023
    Posts:
    102

    good news and bad news, I have fixed it so that my fighter stops walking, I set the WALK_BACK animation to false.

    Code (CSharp):
    1. if (enable)
    2.         {
    3.             // FighterStates currentState = FighterStates.IDLE;
    4.             if (player == PlayerType.HUMAN)
    5.             {
    6.                 UpdateHumanInput();
    7.                 FighterStates currentState = FighterStates.IDLE;
    8.  
    9.             }
    10.          
    11.             else
    12.             {
    13.                 UpdateAiInput();
    14.                 FighterStates currentState = FighterStates.IDLE;
    15.             }
    16.         }
    17.         else if (!enable)
    18.         {
    19.             animator.SetTrigger("IDLE");
    20.             animator.SetBool("WALK_BACK", false);
    21.         }
    and the bad news is that you can only do the booleans, not the triggers, I'll keep looking for the solutions
     
  18. CaptainVega

    CaptainVega

    Joined:
    Jan 26, 2023
    Posts:
    102
    the problem is Ken, because he keeps hitting without waiting for the order to start the round, the animation hits are triggers. Or well if you have a solution that I did not know.
     
    Last edited: Feb 4, 2023
  19. CaptainVega

    CaptainVega

    Joined:
    Jan 26, 2023
    Posts:
    102
    now if there is good news, Ken now if he obeys the order of the combat round.

    Code (CSharp):
    1. public void ResetStats()
    2.     {
    3.         transform.position = StartPoint;
    4.         FighterStates currentState = FighterStates.IDLE;
    5.         healt = MAX_HEALTH;
    6.         enable = false;
    7.         animator.SetFloat("health", 1);
    8.         animator.SetFloat("opponent_health", 1);
    9.  
    10.         animator.SetFloat("random", 0f);
    11.         animator.SetBool("oponent_attacking", false);
    12.         animator.SetFloat("distanceToOponent", 0f);
    13.     }
    If there is a bug that did not work I will try another way, then I will show you the result
     
  20. CaptainVega

    CaptainVega

    Joined:
    Jan 26, 2023
    Posts:
    102
    If I am having a problem that the CELEBRATE animation does not appear, I am trying to solve.
     
  21. CaptainVega

    CaptainVega

    Joined:
    Jan 26, 2023
    Posts:
    102
    now the animation of CELEBRATE is solved.
     
  22. CaptainVega

    CaptainVega

    Joined:
    Jan 26, 2023
    Posts:
    102
    Forget it, the CELEBRATE animation does not appear for Ken, only for Ryu, and I want that after he wins 2 rounds against Ken, he stops restarting the position and that the animation appears if he is dead after 2 rounds, not Idle and also for Ryu. So I solved the animation of CELEBRATE to Ryu.
    Code (CSharp):
    1.  void Update()
    2.     {
    3.         animator.SetFloat("health", healtPercent);
    4.  
    5.         if (oponent != null)
    6.         {
    7.             animator.SetFloat("oponent_health", oponent.healtPercent);
    8.  
    9.  
    10.         }
    11.         else
    12.         {
    13.             animator.SetFloat("oponent_health", 1);
    14.          
    15.         }
    16.  
    17.         if (enable)
    18.         {
    19.             if (player == PlayerType.HUMAN)
    20.             {
    21.                 UpdateHumanInput();
    22.                
    23.  
    24.             }
    25.          
    26.             else
    27.             {
    28.                 UpdateAiInput();
    29.                
    30.             }
    31.         }
    32.  
    33.  
    34.         if (roundWin >= 2)
    35.         {
    36.            
    37.  
    38.             animator.SetFloat("oponent_health", 0);
    39.         }
    40.  
    41.         if (healt <= 0 && currentState != FighterStates.DEAD)
    42.         {
    43.             animator.SetTrigger("DEAD");
    44.  
    45.         }
    46.        
    47.     }
     
  23. CaptainVega

    CaptainVega

    Joined:
    Jan 26, 2023
    Posts:
    102
    if i was forced to create a ResetLife and ResetCpu, so many mess. ok here is my result:cool::D;):).

    :rolleyes:
    I feel that there are even more bugs in the round, and I also want to make an animation come out after two rounds when I win, Ryu hitting the screen or Ken with his Tatsumaki or more, but we better rest the round script in the forum for both bugs . It seems to me that I have to watch many basic videos in unity, and create a post but I better go to rest. I am thinking of creating more scenarios but I will do it just because I already know that part and a menu, if there is a problem I will publish a post. an I'll be back soon:);).
     
  24. dlorre

    dlorre

    Joined:
    Apr 12, 2020
    Posts:
    700
    Well you faced a difficult problem and didn't give up so kudos for that.
     
    CaptainVega likes this.
  25. CaptainVega

    CaptainVega

    Joined:
    Jan 26, 2023
    Posts:
    102
    I knew that there was another bug in the next round.
    when I was pressing the Kill Player1 button on the screen,
    Ken freezes in animation when he wins with the last hit
    until he starts the next round he unfreezes him.

    I don't know how to solve:(.
     
  26. CaptainVega

    CaptainVega

    Joined:
    Jan 26, 2023
    Posts:
    102
    public void Cpu() is that it takes care of restarting when it says YOU LOSE.
    Fighter

    Code (CSharp):
    1. public void Cpu()
    2.     {
    3.         if (player == PlayerType.AI)
    4.         {
    5.             FighterStates currentState = FighterStates.IDLE;
    6.             enable = false;
    7.             animator.SetFloat("random", 0f);
    8.             animator.SetBool("oponent_attacking", false);
    9.             animator.SetFloat("distanceToOponent", 0f);
    10.         }
    11.     }
     
  27. CaptainVega

    CaptainVega

    Joined:
    Jan 26, 2023
    Posts:
    102
    Not only the button on the screen but also if Ken lands a 100% heavy hit on Ryu, Ken freezes until the next round. I'll solve it.
     
  28. CaptainVega

    CaptainVega

    Joined:
    Jan 26, 2023
    Posts:
    102
    I think not the problem of that script, I will look for the cause.
     
  29. CaptainVega

    CaptainVega

    Joined:
    Jan 26, 2023
    Posts:
    102
    I already solved it, but before I go, thank you very much Dlorre for having helped how to do the next round in unity:):D.

    but it's still not resolved, I saw the problem and I think I have to make a new AI, it's because I replaced animator.SetTrigger to animator.SetBool of the animation of "PUNCH_R".
    Fighter (Before Ken's hit animation freezes after he wins the round).
    Code (CSharp):
    1. public void UpdateHumanInput (){
    2.         if (Input.GetAxis ("Horizontal") > 0.1) {
    3.             animator.SetBool ("WALK", true);
    4.         } else {
    5.             animator.SetBool ("WALK", false);
    6.         }
    7.         if (Input.GetAxis ("Horizontal") < -0.1) {
    8.             if (oponent.attacking){
    9.                 animator.SetBool ("WALK_BACK", false);
    10.                 animator.SetBool ("DEFEND", true);
    11.             }else{
    12.                 animator.SetBool ("WALK_BACK", true);
    13.                 animator.SetBool ("DEFEND", false);
    14.             }
    15.         } else {
    16.             animator.SetBool ("WALK_BACK", false);
    17.             animator.SetBool ("DEFEND", false);
    18.         }
    19.         if (Input.GetAxis ("Vertical") < -0.1) {
    20.             animator.SetBool ("DUCK", true);
    21.         } else {
    22.             animator.SetBool ("DUCK", false);
    23.         }
    24.         if (Input.GetKeyDown (KeyCode.UpArrow)) {
    25.             animator.SetTrigger("JUMP");
    26.         }
    27.         if (Input.GetKeyDown (KeyCode.Space)) {
    28.             animator.SetTrigger("PUNCH");
    29.         }
    30.         if (Input.GetKeyDown (KeyCode.K)) {
    31.             animator.SetTrigger("KICK");
    32.         }
    33.         if (Input.GetKeyDown (KeyCode.H)) {
    34.             animator.SetTrigger("HADOKEN");
    35.         }
    36.     }
    37.  
    Fighter Now.
    Code (CSharp):
    1. public void UpdateHumanInput()
    2.     {
    3.      
    4.             bool moverseoprimido = Input.GetKey(KeyCode.RightArrow);
    5.  
    6.  
    7.  
    8.         if (moverseoprimido && speed < 1.0f)
    9.         {
    10.             speed += Time.deltaTime * acceleration;
    11.         }
    12.         if (!moverseoprimido && speed > 0.0f)
    13.         {
    14.             speed -= Time.deltaTime * deceleration;
    15.         }
    16.         if (!moverseoprimido && speed < 0.0f)
    17.         {
    18.             speed = 0.0f;
    19.         }
    20.         if (Input.GetAxis("Horizontal") < -0.1)
    21.         {
    22.  
    23.             if (oponent.attacking)
    24.             {
    25.                 animator.SetBool("WALK_BACK", false);
    26.                 animator.SetBool("DEFEND", true);
    27.             }
    28.             else
    29.             {
    30.                 animator.SetBool("WALK_BACK", true);
    31.                 animator.SetBool("DEFEND", false);
    32.             }
    33.         }
    34.         else
    35.         {
    36.             animator.SetBool("WALK_BACK", false);
    37.             animator.SetBool("DEFEND", false);
    38.         }
    39.  
    40.         if (Input.GetAxis("Vertical") < -0.1)
    41.         {
    42.             animator.SetBool("DUCK", true);
    43.         }
    44.         else
    45.         {
    46.             animator.SetBool("DUCK", false);
    47.         }
    48.  
    49.         if (Input.GetKeyDown(KeyCode.UpArrow))
    50.         {
    51.             animator.SetTrigger("JUMP");
    52.         }
    53.  
    54.         if (Input.GetKeyDown(KeyCode.Space))
    55.         {
    56.             animator.SetBool("attackOn", true);
    57.             animator.SetTrigger("PUNCH_R");
    58.             hitColider1.punch =+ 50f;
    59.         }
    60.         else
    61.         {
    62.             animator.SetBool("attackOn", false);
    63.         }
    64.         if (Input.GetKeyDown(KeyCode.U))
    65.         {
    66.             animator.SetTrigger("PUNCH_R");
    67.             hitColider2.punch =+ 2;
    68.         }
    69.         if (Input.GetKeyDown(KeyCode.K))
    70.         {
    71.             animator.SetTrigger("KICK_R");
    72.         }
    73.  
    74.         if (Input.GetKeyDown(KeyCode.Space))
    75.         {
    76.             animator.SetTrigger("HADOKEN");
    77.             animator.SetBool("SpecialMoves", true);
    78.         }
    79.         else
    80.         {
    81.             animator.SetBool("SpecialMoves", false);
    82.         }
    83.  
    84.  
    85.         animator.SetFloat("Walk", speed);
    86.      
    87.     }
    that's why Ken's animation freezes, which is very strange because this is his AI

    Code (CSharp):
    1. public void UpdateAiInput()
    2.     {
    3.      
    4.         animator.SetBool("defending", defending);
    5.         //animator.SetBool ("invulnerable", invulnerable);
    6.         //animator.SetBool ("enable", enable);
    7.  
    8.         animator.SetBool("oponent_attacking", oponent.attacking);
    9.         animator.SetFloat("distanceToOponent", getDistanceToOponennt());
    10.  
    11.         if (Time.time - randomSetTime > 1)
    12.         {
    13.             random = Random.value;
    14.             randomSetTime = Time.time;
    15.         }
    16.         animator.SetFloat("random", random);
    17.  
    18.      
    19.     }
    I don't know if I have to create a new AI or there is a solution, I don't feel like creating a new post, I'd better leave it like that and now if I'm going to rest and this post, yes, it may be that I return to this next round post but not now.
     
    Last edited: Feb 15, 2023
  30. dlorre

    dlorre

    Joined:
    Apr 12, 2020
    Posts:
    700
    To solve these kind of issues there are 2 main ways. One is to use the debugger in visual studio which is a bit complicated but gives you every info you need. The second is to use Debug.Log() messages in your code so you can figure out what's going on.

    They both have pros and cons. By using the debugger you can place breakpoints and watch variable content. But you need to learn how to use it and attach it to Unity. Debug.Log() statements can be tedious to write but you can get them directly in the console window and in the Player.log file.

    For example you can use debug statements like that:

    Code (csharp):
    1.  
    2. Debug.Log("entering function Cpu()") ;
    3.  
    4. Debug.Log($"speed={speed}") ;
    5.  
    6. Debug.Log($"new random value={random}") ;
    7.  
    8. Debug.Log($"opponent attacking={opponent.attacking}") ;
    9.  
    That way you can figure out what's going on with your variables.

     
    Last edited: Feb 16, 2023
  31. CaptainVega

    CaptainVega

    Joined:
    Jan 26, 2023
    Posts:
    102
    thank you, I'll try later. I am going to create a new project to put into practice the basics of unity so that I can learn the basics, I am going to watch many basic videos on unity, As I already told you, I will return to this post one day:).
     
    Last edited: Feb 16, 2023
    dlorre likes this.
  32. CaptainVega

    CaptainVega

    Joined:
    Jan 26, 2023
    Posts:
    102
    I just discovered that it wasn't because if he wins the round the hit animation freezes, in combat the hit animation also freezes, only the first hit freezes but the second hit doesn't freeze, ken takes advantage of the fact that freeze the first hit animation to follow the next, I put this Fighter script.
    Code (CSharp):
    1.  public void UpdateAiInput()
    2.     {
    3.        
    4.         animator.SetBool("defending", defending);
    5.         //animator.SetBool ("invulnerable", invulnerable);
    6.         //animator.SetBool ("enable", enable);
    7.  
    8.         animator.SetBool("oponent_attacking", oponent.attacking);
    9.         animator.SetFloat("distanceToOponent", getDistanceToOponennt());
    10.  
    11.         if (Time.time - randomSetTime > 1)
    12.         {
    13.             random = Random.value;
    14.             randomSetTime = Time.time;
    15.         }
    16.         animator.SetFloat("random", random);
    17.     // I put the following  
    18. if (Input.GetKeyDown(KeyCode.L))
    19.         {
    20.             animator.SetTrigger("PUNCH_L");
    21.         }
    22.  
    23.     }
    There the animation does not freeze when I press "L", (when I play an AI the animation of the first hit freezes, I don't know how to solve it), if I have another problem for the next round but more later, when I give up how to do that no freeze animation ken's first hit can i know i created a post.
     
  33. dlorre

    dlorre

    Joined:
    Apr 12, 2020
    Posts:
    700
    All I can tell by looking at this script is that your animator receives a new random value every second.
     
  34. CaptainVega

    CaptainVega

    Joined:
    Jan 26, 2023
    Posts:
    102
    could be.
     
  35. CaptainVega

    CaptainVega

    Joined:
    Jan 26, 2023
    Posts:
    102
    Oh I forgot to tell you, it also slows down Ken's hit animation, that's why the hit animation freezes when all floats are at zero.

    in the video you will notice ken's punch animation several times slows down the punch animation. until it stays at 0 float.
     
  36. dlorre

    dlorre

    Joined:
    Apr 12, 2020
    Posts:
    700
    I mean you won't find the solution in this script because it sets 3 things: opponent attacking, distance to opponent and random. It is something else that triggers the attack, or it is based on random but you need to figure how it is working.
     
  37. CaptainVega

    CaptainVega

    Joined:
    Jan 26, 2023
    Posts:
    102
    Oh:( ok:(. So I'll keep looking for the solution, Or else, I'll create a post when I give up.
     
  38. dlorre

    dlorre

    Joined:
    Apr 12, 2020
    Posts:
    700
    CaptainVega likes this.
  39. CaptainVega

    CaptainVega

    Joined:
    Jan 26, 2023
    Posts:
    102
    Wow:eek:, I didn't know that.
     
  40. CaptainVega

    CaptainVega

    Joined:
    Jan 26, 2023
    Posts:
    102
    the only problem is where i have to put that in AI:(.
    Code (CSharp):
    1.  public void UpdateAiInput()
    2.     {
    3.      
    4.         animator.SetBool("defending", defending);
    5.         //animator.SetBool ("invulnerable", invulnerable);
    6.         //animator.SetBool ("enable", enable);
    7.         animator.SetBool("oponent_attacking", oponent.attacking);
    8.         animator.SetFloat("distanceToOponent", getDistanceToOponennt());
    9.         if (Time.time - randomSetTime > 1)
    10.         {
    11.             random = Random.value;
    12.             randomSetTime = Time.time;
    13.         }
    14.         animator.SetFloat("random", random);
    15.    // I won't use this because it's AI but I'll try it.
    16. if (Input.GetKeyDown(KeyCode.L))
    17.         {
    18.             animator.SetTrigger("PUNCH_L");
    19.         }
    20.     }
     
  41. SF_FrankvHoof

    SF_FrankvHoof

    Joined:
    Apr 1, 2022
    Posts:
    780
    Any trigger that's picked up by the Animator will be reset. But any trigger that isn't will not.
    So when setting a new trigger you'll usually want to reset all others.
     
  42. CaptainVega

    CaptainVega

    Joined:
    Jan 26, 2023
    Posts:
    102
    Thank you both very much, the truth is that I didn't know that there is a function called ResetTrigger, I was looking for a way to disable the animation by Trigger when the next round began, I had to use bool for Type Human.
     
  43. CaptainVega

    CaptainVega

    Joined:
    Jan 26, 2023
    Posts:
    102
    I will still continue looking for a way so that Ken's animation does not freeze because it is AI.
     
  44. CaptainVega

    CaptainVega

    Joined:
    Jan 26, 2023
    Posts:
    102
    There is a curiosity here, why when I put a number to float 10f to punch it starts to slow down and when I put 0f it starts a normal speedo_O? Can someone explain to me why? I don't get it.
    Code (CSharp):
    1. using System.Collections;
    2. using UnityEngine;
    3.  
    4. public class HitColider : MonoBehaviour
    5. {
    6.     public string punchName;
    7.     public float punch;
    8.  
    9.     public Fighter owner;
    10.  
    11.     void OnTriggerEnter(Collider other)
    12.     {
    13.         Fighter somebody = other.gameObject.GetComponent<Fighter>();
    14.         if (owner.attacking)
    15.         {
    16.             if (somebody != null && somebody != owner)
    17.             {
    18.                 somebody.hurt(punch);
    19.             }
    20.  
    21.         }
    22.    
    23.  
    24.     }
    25.  
    26.  
    27. }
    now if ken's animation no longer freezes but that's sad for ken:(.
    and a very rare bugo_O.
     
  45. dlorre

    dlorre

    Joined:
    Apr 12, 2020
    Posts:
    700
    To debug that stuff put your cursor on Fighter and press F12 then you will see what hurt does. Or even press directly F12 with the cursor on hurt.
     
    Last edited: Feb 21, 2023
  46. CaptainVega

    CaptainVega

    Joined:
    Jan 26, 2023
    Posts:
    102
    I have not found anything.
    just that.
    Code (CSharp):
    1. public virtual void hurt(float damage)
    2.     {
    3.         if (!invulnerable)
    4.         {
    5.             if (defending)
    6.             {
    7.                 damage *= 0.2f;
    8.             }
    9.             if (healt >= damage)
    10.             {
    11.                 healt -= damage;
    12.             }
    13.             else
    14.             {
    15.                 healt = 0;
    16.             }
    17.  
    18.             if (healt > 0)
    19.             {
    20.                 animator.SetTrigger("TAKE_HIT");
    21.             }
    22.  
    23.  
    24.  
    25.  
    26.         }
    27.     }
    I have not found what is the cause.
     
  47. dlorre

    dlorre

    Joined:
    Apr 12, 2020
    Posts:
    700
    That's when you use Debug.Log:

    Code (csharp):
    1.  
    2.     public virtual void hurt(float damage)
    3.         {
    4.            Debug.Log($"hurt with damage {damage} invulnerable={invulnerable} health={healt}");
    5.             if (!invulnerable)
    6.             {
    7.                 if (defending)
    8.                 {                
    9.                     damage *= 0.2f;
    10.                    Debug.Log("is defending damage is now {damage}") ;
    11.                 }
    12.                 if (healt >= damage)
    13.                 {
    14.                     healt -= damage;
    15.                 }
    16.                 else
    17.                 {
    18.                     healt = 0;
    19.                 }
    20.                Debug.Log($"health is now {healt}") ;
    21.                 if (healt > 0)
    22.                 {
    23.                    Debug.Log("taking a hit");
    24.                     animator.SetTrigger("TAKE_HIT");
    25.                 }
    26.      
    27.             }
    28.         }
    29.  
    30.  
    However my guess is that when you set hurt to 0 TAKE_HIT is triggered but when you set it to 10f it is not since healt becomes 0.
     
  48. CaptainVega

    CaptainVega

    Joined:
    Jan 26, 2023
    Posts:
    102
    how would i do it:confused: (to resolve)? I don't know:D. I really don't know how I will solve it without causing the bugs.
     
  49. dlorre

    dlorre

    Joined:
    Apr 12, 2020
    Posts:
    700
    I'm not sure what is your bug? All I can tell is that if you want to set a trigger when health is 0 then just do that.

    Code (csharp):
    1.  
    2.                 if (healt > 0)
    3.                 {
    4.                     animator.SetTrigger("TAKE_HIT");
    5.                 }
    6.                else {
    7.                     animator.SetTrigger("IS_DEAD_OR_SOMETHING");
    8.                }
    9.  
     
  50. CaptainVega

    CaptainVega

    Joined:
    Jan 26, 2023
    Posts:
    102
    not yet, still the same problem that Ken's animation hit slows down, luckily I have a backup copy before Ken's animation hit freezes. to see what the problem is that I forgot that my backup copy was not deleted:).