Search Unity

Change animation or sprite in game2d

Discussion in '2D' started by ba01, Sep 19, 2014.

  1. ba01

    ba01

    Joined:
    May 8, 2014
    Posts:
    10
    I need to deform main user in game when main user gets special items.

    example:
    + i have main user with animation move left and move right. (it is a man).
    + i have other object with animation move left and move right. (it is a tiger).
    + i have other object with animation move left and move right. (it is a elephant).

    When main user get "tiger item", main user is replaced by tiger animation
    When main user get "elephant item", main user is replaced by elephant animation

    How can i implement it? make sure that animators move left right is OK when main user is replaced by other animation.

    Thanks so much.
     
  2. ba01

    ba01

    Joined:
    May 8, 2014
    Posts:
    10
    Please to help me. Thanks
     
  3. GarBenjamin

    GarBenjamin

    Joined:
    Dec 26, 2013
    Posts:
    7,441
    Okay so you have a player object that can transform into a tiger or an elephant by collecting power ups.

    You need to track the current transformation state of your player (its current form), an enumeration is handy for this:

    Code (CSharp):
    1. // This enumeration tracks the 3 possible forms the player can transform into
    2. public enum PlayerTransformationType
    3. {
    4.     Human = 0,
    5.     Tiger = 1,
    6.     Elephant = 2
    7. }
    If I understand you correctly, you have set up 3 prefabs. One for the human form. One for the elephant form. One for the Tiger form. Based on that, in your player script you will need a way to track the current form the player is in and you will have an array of GameObjects, something like this:

    Code (CSharp):
    1.  // This tracks which form the player is currently in
    2. private PlayerTransformationType eCurrentForm;
    3.  
    4. // This array holds references to the 3 different form prefabs (objects)
    5. // you have created. NOTE: The actual array is loaded from the inspector
    6. public GameObject[] goFormPrefabs;
    Then in the inspector for the player script you can set the size of the array to 3 and then you can specify each of the prefabs you have defined being sure to set the first element in the array to your Human prefab, the second element in the array to your Tiger prefab and the 3rd element in the array to your Elephant prefab.

    Since you have used 3 different objects (again I am assuming you mean you created 3 different prefabs) you will need to make a 4th object as it will be used as the actual player object as far as tracking state, position and so forth. You will need to set your 3 transformations (the various forms) objects as children of this main player object. Unlike those prefabs, this main player object will not have a renderer of any kind.

    Then you can code up the movement as usual using keys to move left and right to move your main player object which will also cause the other 3 objects to move.

    You can then test this by adding some code to the Start() of your player script to set the current Form and activate the appropriate Form game object, something like this:
    Code (CSharp):
    1. // Activate Human Form
    2. eCurrentForm = PlayerTransformationType.Human;
    3. goFormPrefabs[0].SetActive(true);
    4. goFormPrefabs[1].SetActive(false);
    5. goFormPrefabs[2].SetActive(false);
    6.  
    7. // Uncomment the next 4 lines to Activate Tiger Form
    8. // eCurrentForm = PlayerTransformationType.Tiger;
    9. // goFormPrefabs[0].SetActive(false);
    10. // goFormPrefabs[1].SetActive(true);
    11. // goFormPrefabs[2].SetActive(false);
    12.  
    13. // Uncomment the next 4 lines to Activate Elephant Form
    14. // eCurrentForm = PlayerTransformationType.Elephant;
    15. // goFormPrefabs[0].SetActive(false);
    16. // goFormPrefabs[1].SetActive(false);
    17. // goFormPrefabs[2].SetActive(true);
    18.  
    I feel like I should say this is not the method I would use. All of this is just based on trying to work with the way you have set things up so far.

    Hopefully this will at least get you started.
     
    ba01 likes this.