# How do I make my character change his ability's when his sprite changes

I am making a platformer and am currently trying to have it so that my character will have different powers based on his emotions, and his sprite will change to match those emotions.

I have got it so that his sprite will change but I don't know how to get his powers to change as well. Note that I haven't even made his other powers yet, but even if I did I don't know how to change his ability's with his sprites as I am still very new to this whole coding thing. Can anyone please help me?

Here is my code for changing his sprites, if that would help.

using UnityEngine;
using System.Collections;

public class MoodManager : MonoBehaviour {

public Sprite sprite1;// happy//
public Sprite sprite2;// angry//
public Sprite sprite4;//default//

private SpriteRenderer spriteRenderer;

void Start ()
{
spriteRenderer = GetComponent<SpriteRenderer>(); // we are accessing the SpriteRenderer that is attached to the Gameobject
if (spriteRenderer.sprite == null) // if the sprite on spriteRenderer is null then
spriteRenderer.sprite = sprite1; // set the sprite to sprite1
}

void Update ()
{
if (Input.GetKeyDown (KeyCode.Alpha1)) // If the space bar is pushed down
ChangeMoodHappy (); // call method to change sprite

if (Input.GetKeyDown (KeyCode.Alpha2)){
ChangeMoodAngry ();

if (Input.GetKeyDown (KeyCode.Alpha3)){
}

void ChangeMoodHappy (){
if (spriteRenderer.sprite == sprite1) // if the spriteRenderer sprite = sprite1 then change to sprite2
spriteRenderer.sprite = sprite4;
else
spriteRenderer.sprite = sprite1; // otherwise change it back to sprite1
}

void ChangeMoodAngry (){
if (spriteRenderer.sprite == sprite2) // if the spriteRenderer sprite = sprite1 then change to sprite2
spriteRenderer.sprite = sprite4;
else
spriteRenderer.sprite = sprite2; // otherwise change it back to sprite1
}

if (spriteRenderer.sprite == sprite3) // if the spriteRenderer sprite = sprite1 then change to sprite2
spriteRenderer.sprite = sprite4;
else
spriteRenderer.sprite = sprite3; // otherwise change it back to sprite1
}
}

}

• What kind of "powers" are you talking about? – Philipp Nov 7 '15 at 2:25

I would have a look at Unity's animation system, Mecanim (Unity has a great set of resources available on the subject).

Essentially, for what you're trying to do, I would set up a few states in my character's animator controller, representing the character's moods. You can assign a sprite (or sequence of sprites) as the animation for a given state, and get what your code is currently doing basically for free.

Here's a preview of what your state machine might look like:

The "mood" parameter (shown on the left) is just an integer that corresponds to an enum I've defined in the MoodManager component, and the state transitions are simply "mood equals [some value]" (again, as defined in the enum). The MoodManager component becomes pretty simple:

using UnityEngine;

public class MoodManager : MonoBehaviour
{
public enum Mood
{
Default = 0,
Happy = 1,
Angry = 3
}

public Mood mood;
Animator animator;

void Awake ()
{
animator = GetComponent<Animator> ();
}

void Update ()
{
if (Input.GetKeyDown (KeyCode.Alpha1)) {
mood = Mood.Happy;
} else if (Input.GetKeyDown (KeyCode.Alpha2)) {
} else if (Input.GetKeyDown (KeyCode.Alpha3)) {
mood = Mood.Angry;
} else if (Input.GetKeyDown (KeyCode.Alpha0)) {
mood = Mood.Default;
}

animator.SetInteger ("mood", (int)mood);
}
}


You now have your character's mood available to use in driving abilities, probably from another component. The particulars are really dependent on how your game should function, but in the simplest case, you could do a switch on the character's current move whenever the mouse is clicked (or however your abilities are triggered), and execute the correct move based on that value.

you can set your sprites and powers to be Arrays instead of different public sprites and change them accordingly. you can loop thru and set them with a for loop but in this case you can easily change them 0-3 for example.

public Sprite[] Moods;//set mood sprites in order 0-3
public GameObject[] Powers; //set powers 0-3 these can be gameobject prefabs like fireball, ice, any power you want ..

GameObject currentPower; //set power to default in Start()

void Start(){