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. Join us on Dec 8, 2022, between 7 am & 7 pm EST, in the DOTS Dev Blitz Day 2022 - Q&A forum, Discord, and Unity3D Subreddit to learn more about DOTS directly from the Unity Developers.
    Dismiss Notice
  3. 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

Trying to attach animation for when selecting amount of players (Local Multiplayer)

Discussion in 'Scripting' started by unity_1851CAA243AE5759EA1B, Jun 15, 2021.

  1. unity_1851CAA243AE5759EA1B

    unity_1851CAA243AE5759EA1B

    Joined:
    Jun 5, 2021
    Posts:
    1
    Hey people-with-significantly-more-experience,

    I'm currently trying to modify a code used for character selection for players to use to select how many people are playing and want to have the player amount icon displayed as a short animation. I have the animation but I can't seem to get it attached to the code as I want it. The code is as follows:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using TMPro;
    4. using UnityEngine;
    5. using UnityEngine.UI;
    6.  
    7. public class PlayerNumberSelection: MonoBehaviour
    8. {
    9.     private int selectedPlayersIndex;
    10.  
    11.     [Header("Amount of Players")]
    12.     [SerializeField] private List<PlayerSelectObject> PlayerList = new List<PlayerSelectObject>();
    13.  
    14.     [Header("UI Reference")]
    15.     [SerializeField] private Text PlayerNumber;
    16.     [SerializeField] private Animation PlayerNumberAnimation;
    17.  
    18.     private void Start()
    19.     {
    20.         UpdateCharacterSelectionUI();
    21.     }
    22.  
    23.     public void LeftArrow()
    24.     {
    25.         selectedPlayersIndex--;
    26.         if (selectedPlayersIndex < 0)
    27.             selectedPlayersIndex = PlayerList.Count - 1;
    28.  
    29.         UpdateCharacterSelectionUI();
    30.     }
    31.  
    32.     public void RightArrow()
    33.     {
    34.         selectedPlayersIndex++;
    35.         if (selectedPlayersIndex == PlayerList.Count)
    36.             selectedPlayersIndex = 0;
    37.  
    38.         UpdateCharacterSelectionUI();
    39. }
    40.  
    41.     public void Confirm()
    42.     {
    43.         Debug.Log(string.Format("{0}:{1} Players", selectedPlayersIndex, PlayerList[selectedPlayersIndex].PlayerNumber));
    44.     }
    45.  
    46. private void UpdateCharacterSelectionUI()
    47.     {
    48.         // Animation , Name
    49.         PlayerNumberAnimation.animateOnlyIfVisible = PlayerList[selectedPlayersIndex].sprite;
    50.         PlayerNumber.text = PlayerList[selectedPlayersIndex].PlayerNumber;
    51.     }
    52.  
    53.     [System.Serializable]
    54.     public class PlayerSelectObject
    55.     {
    56.         public Animation sprite;
    57.         public string PlayerNumber;
    58.     }
    59. }


    I'm also in need of some tips on how to make it apply the number of players to the Character Selection screen that follows so that the players can choose their character and have that applied to their player profile which, truthfully, I have no clue how to do.

    The idea for that would be a sort of if->then system with it being:
    if x players-> characters selection occurs x times and is applied to players respectively

    I'm brand-new to all of this and figuring it out as I go so any pointers would be great. And if something requires further explanation just let me know. Thanks in advance!
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    29,725
    I see you using bare Animations. You can of course still do this but the reference Unity way nowadays is to use an Animation Controller and set up a state table, then you drive the state table directly with properties, and let it figure out how to animate stuff.

    The Animation Controller system is broadly referred to as MecAnim in Unity and there are thousands of tutorials out there for it. Hie thee to Youtube and get crackin'.