0
\$\begingroup\$

enter image description here enter image description here

Above is the attachment of my mecanim state, hierarchy and folder in Animator.

The problem I'm facing now is fail to change multiple sprite mecanim state using C# script. When I move my sprite, I can successfully change it from idle to backwalk state, however it limit my changes solely to backwalk state but not all other states. I want to make it like when I press right arrow key it change to rightwalk state, while press left arrow key to change it to leftwalk state and so on. Unfortunately the if else statement that I declare is not working making my changing movement only stick to one state. I wonder what mistake I made, and how I can fix my code?

I'm a newbie to Unity and not familiar with C#. I just learn it from the official Unity tutorials series. I been searching for many related question regard of the problem but still I can't figure out the solution. This is the first question I ask at Game Stack Exchange, sorry if I violate any rules and please guide me through it. Appreciate anyone help sincerely.

Here I attach my code:

using UnityEngine;
using System.Collections;

public class CharacterMove : MonoBehaviour
{
    public float MoveSpeed = 1;
    float aspectspeedx = Screen.width / 1366;
    float aspectspeedy = Screen.height / 768;

// Use this for initialization
void Start()
{
    GameObject anim = GameObject.Find("taoist");
    anim.GetComponent<Animator>();
}

// Update is called once per frame
void Update()
{
    float MoveSpeed = 1f;
    // change the string to your parameter     

    string characterfrontwalk = "taoist_front_walk";
    string characterbackwalk = "taoist_back_walk";
    string characterleftwalk = "taoist_left_walk";
    string characterrightwalk = "taoist_right_walk";
    /* if you want corner walk 
    string characterrightfrontwalk = "taoist_right_front_walk";
    string characterrightbackwalk = "taoist_right_back_walk";
    string characterleftfrontwalk = "taoist_left_front_walk";
    string characterleftbackwalk = "taoist_left_back_walk";
    // to use this corner setting , you must initialize yourself 

    */
    GameObject character = GameObject.Find("taoist");
    GameObject theanim = GameObject.Find("taoist");
    theanim.GetComponent<Animator>();
    theanim.GetComponent<Animator>().SetBool(characterfrontwalk, true);
    theanim.GetComponent<Animator>().SetBool(characterbackwalk, false);
    theanim.GetComponent<Animator>().SetBool(characterleftwalk, false);
    theanim.GetComponent<Animator>().SetBool(characterrightwalk, false);
    character.transform.localPosition = new Vector3(character.transform.localPosition.x, character.transform.localPosition.y + 0.05f * MoveSpeed, character.transform.localPosition.z);
    character.transform.localPosition = new Vector3(character.transform.localPosition.x - 0.05f * MoveSpeed, character.transform.localPosition.y, character.transform.localPosition.z);

    if (!(Input.GetKey("up") && Input.GetKey("right")) && !(Input.GetKey("up") && Input.GetKey("left")) && !(Input.GetKey("down") && Input.GetKey("right")) && !(Input.GetKey("down") && Input.GetKey("left")))
    {
        if (Input.GetKey("up"))
        {
            theanim.GetComponent<Animator>().SetBool(characterfrontwalk, true);
            theanim.GetComponent<Animator>().SetBool(characterbackwalk, false);
            theanim.GetComponent<Animator>().SetBool(characterleftwalk, false);
            theanim.GetComponent<Animator>().SetBool(characterrightwalk, false);

            character.transform.localPosition = new Vector3(character.transform.localPosition.x, character.transform.localPosition.y + 0.05f * MoveSpeed, character.transform.localPosition.z);
        }

        if (Input.GetKey("down"))
        {
            theanim.GetComponent<Animator>().SetBool(characterfrontwalk, false);
            theanim.GetComponent<Animator>().SetBool(characterbackwalk, true);
            theanim.GetComponent<Animator>().SetBool(characterleftwalk, false);
            theanim.GetComponent<Animator>().SetBool(characterrightwalk, false);

            character.transform.localPosition = new Vector3(character.transform.localPosition.x, character.transform.localPosition.y - 0.05f * MoveSpeed, character.transform.localPosition.z);
        }

        if (Input.GetKey("left"))
        {
            theanim.GetComponent<Animator>().SetBool(characterfrontwalk, false);
            theanim.GetComponent<Animator>().SetBool(characterbackwalk, false);
            theanim.GetComponent<Animator>().SetBool(characterleftwalk, true);
            theanim.GetComponent<Animator>().SetBool(characterrightwalk, false);

            character.transform.localPosition = new Vector3(character.transform.localPosition.x - 0.05f * MoveSpeed, character.transform.localPosition.y, character.transform.localPosition.z);
        }

        if (Input.GetKey("right"))
        {
            theanim.GetComponent<Animator>().SetBool(characterfrontwalk, false);
            theanim.GetComponent<Animator>().SetBool(characterbackwalk, false);
            theanim.GetComponent<Animator>().SetBool(characterleftwalk, false);
            theanim.GetComponent<Animator>().SetBool(characterrightwalk, true);

            character.transform.localPosition = new Vector3(character.transform.localPosition.x + 0.05f * MoveSpeed, character.transform.localPosition.y, character.transform.localPosition.z);
        }

    }

}

    /* 

    // THIS CODE IS FOR CORNER MOVE , UN-MODIFIED , because im too tired to modify it sorry , you need to think yourself ;) 

    if( Input.GetKey("down") && Input.GetKey("left")){

        theanim.GetComponent<Animator> ().SetBool ("MoveUp", false);
        theanim.GetComponent<Animator> ().SetBool ("MoveDown", false);
        theanim.GetComponent<Animator> ().SetBool ("MoveLeft", false);
        theanim.GetComponent<Animator> ().SetBool ("MoveRight", false);
        theanim.GetComponent<Animator> ().SetBool ("MoveLeftUp", false);
        theanim.GetComponent<Animator> ().SetBool ("MoveLeftDown", true);
        theanim.GetComponent<Animator> ().SetBool ("MoveRightDown", false);
        theanim.GetComponent<Animator> ().SetBool ("MoveRightUp", false);
        theanim.GetComponent<Animator> ().SetBool ("NotMoving", false);
        character.transform.localPosition = new Vector3 (character.transform.localPosition.x, character.transform.localPosition.y - 0.05f * MoveSpeed, character.transform.localPosition.z);
        character.transform.localPosition = new Vector3 (character.transform.localPosition.x - 0.05f * MoveSpeed, character.transform.localPosition.y, character.transform.localPosition.z);

    }


    if( Input.GetKey("up") && Input.GetKey("right")){

        theanim.GetComponent<Animator> ().SetBool ("MoveUp", false);
        theanim.GetComponent<Animator> ().SetBool ("MoveDown", false);
        theanim.GetComponent<Animator> ().SetBool ("MoveLeft", false);
        theanim.GetComponent<Animator> ().SetBool ("MoveRight", false);
        theanim.GetComponent<Animator> ().SetBool ("MoveLeftUp", false);
        theanim.GetComponent<Animator> ().SetBool ("MoveLeftDown", false);
        theanim.GetComponent<Animator> ().SetBool ("MoveRightDown", false);
        theanim.GetComponent<Animator> ().SetBool ("MoveRightUp", true);
        theanim.GetComponent<Animator> ().SetBool ("NotMoving", false);
        character.transform.localPosition = new Vector3 (character.transform.localPosition.x, character.transform.localPosition.y + 0.05f * MoveSpeed, character.transform.localPosition.z);
        character.transform.localPosition = new Vector3 (character.transform.localPosition.x + 0.05f * MoveSpeed, character.transform.localPosition.y, character.transform.localPosition.z);

    }
    if( Input.GetKey("down") && Input.GetKey("right")){

        theanim.GetComponent<Animator> ().SetBool ("MoveUp", false);
        theanim.GetComponent<Animator> ().SetBool ("MoveDown", false);
        theanim.GetComponent<Animator> ().SetBool ("MoveLeft", false);
        theanim.GetComponent<Animator> ().SetBool ("MoveRight", false);
        theanim.GetComponent<Animator> ().SetBool ("MoveLeftUp", false);
        theanim.GetComponent<Animator> ().SetBool ("MoveLeftDown", false);
        theanim.GetComponent<Animator> ().SetBool ("MoveRightDown", true);
        theanim.GetComponent<Animator> ().SetBool ("MoveRightUp", false);
        theanim.GetComponent<Animator> ().SetBool ("NotMoving", false);
        character.transform.localPosition = new Vector3 (character.transform.localPosition.x, character.transform.localPosition.y - 0.05f * MoveSpeed, character.transform.localPosition.z);
        character.transform.localPosition = new Vector3 (character.transform.localPosition.x + 0.05f * MoveSpeed, character.transform.localPosition.y, character.transform.localPosition.z);

    } 
    // Un-MODIFIED corner setting , initialize yourself 
        */



}

public class LastDirection
{
    public string thelastdirection;
}
\$\endgroup\$
1
\$\begingroup\$

Instead of editing my last answer , i now really understand your problem :) , your first statement is look like this ( and its wrong ) :

Wrong

As you see , your statement cannot go to other direction because they don't have the way , and the only way to go to other statement is through idle .

Then , how to fix it ? its simple , you must create their way to other direction so they don't need to go idle first to go "there" ( the other direction ) :

Right

First , sorry for bad draw because i used paint ;) .

What i try to mean in the picture is , that each of your statement must connect to all other statement with its own condition , and not only connect to idle state , i know it is a long job and can take an hour but you must do it if you want .

Well , goodluck in your project :) , i can sure you will meet other problem that i have too here and you can ask me how-to-fix it ;) , and i can help your project if you want :) .

EDIT :

Yesterday you upload the picture of the statement like this :

wrong

But honestly , its not what called " connect each statement to all statement " , but what i was trying to mean is the statement must connect like this :

work

\$\endgroup\$
  • \$\begingroup\$ Comments are not for extended discussion; this conversation has been moved to chat. \$\endgroup\$ – Jesse Dorsey Nov 28 '16 at 16:41
1
\$\begingroup\$

Instead of editing my last 2 answer ,i will just create new :p .

I fixed your character move animation .

It used new script , and new ability is added :
- Character Moving script without vertical and horizontal

Preview :

https://www.youtube.com/watch?v=b3T7xROtJ3A&feature=youtu.be

The animation play well except the animation it-self i think , you need to make the animation become better ;) .

How - To - Import :

  1. Download the file - > https://goo.gl/3Dt0HQ , if the link dead tell me

  2. If you don't want to import inside your project , create new project as test-project so you can assure it won't destroy your current project

  3. Open scene at asset , the scene is named "sceneone"

4. Try to play it and learn the different with your current script :)


In case the scene and script not imported :

  1. Create empty scene

  2. Create two empty object , one called " taoistmoverobject " and " taoist "

  3. Put taoist object inside taoistmoverobject scene

  4. Find the animator and the script then tag it as component at " taoistmoverobject "

  5. ( Optional ) Put camera inside " taoistmoverobject " if you want to make camera follow when character moving


That all ;) , don't forget to learn from it :)

\$\endgroup\$
  • \$\begingroup\$ This should fix your problem :) , if you have any problem just ask me ;) , or if this doesn't work i will try another way \$\endgroup\$ – user6668201 Nov 28 '16 at 16:09
  • \$\begingroup\$ Studying it now \$\endgroup\$ – Philip Chow Nov 28 '16 at 16:11
  • \$\begingroup\$ if it work , mark it as solve , if doesn't work , don't mark it and tell if its not work ;) , im open for question at facebook either email \$\endgroup\$ – user6668201 Nov 28 '16 at 16:12
  • \$\begingroup\$ Well ... im sleepy though , i will take a sleep now , i go back about .... 15 hour later . Any question just email me . \$\endgroup\$ – user6668201 Nov 28 '16 at 16:13
  • \$\begingroup\$ good :) , i know its hard but keep learning ;) . \$\endgroup\$ – user6668201 Nov 29 '16 at 12:18
0
\$\begingroup\$

I know what problem was going on because i had the trouble with it too , the problem is at both side , the scripting and animator .

To make it instantly change like from left instantly to right , you must connect / put transition between all animation with it own condition .

Not just like from idle -> right , but also right -> left , right -> down , right -> up and ect .

Never use vertical and horizontal method , its not work the best , just detect which button the user hold and do the movement .

The preview is look like this :

https://www.youtube.com/embed/rivOHgxp2Cw
or
https://www.youtube.com/watch?v=rivOHgxp2Cw

and this is the script i used for your example ;) learn how the script work and watch the preview video .

using UnityEngine;
using System.Collections;

public class CharacterMove : MonoBehaviour {
	public float MoveSpeed = 1;
	float aspectspeedx = Screen.width / 1366;
	float aspectspeedy = Screen.height / 768;

	// Use this for initialization
	void Start () {
		GameObject anim = GameObject.Find("Character");
		anim.GetComponent<Animator> ();
	}
		
	// Update is called once per frame
	void Update () {
		LastDirection lastdirectionvar = new LastDirection ();
		GameObject character = GameObject.Find("Character");
		GameObject theanim = GameObject.Find("Character");
		theanim.GetComponent<Animator> ();
		/*
		 
		 Change one of them to true
		 
		 	theanim.GetComponent<Animator> ().SetBool ("MoveUp", false);
			theanim.GetComponent<Animator> ().SetBool ("MoveDown", false);
			theanim.GetComponent<Animator> ().SetBool ("MoveLeft", false);
			theanim.GetComponent<Animator> ().SetBool ("MoveRight", false);
			theanim.GetComponent<Animator> ().SetBool ("MoveLeftUp", false);
			theanim.GetComponent<Animator> ().SetBool ("MoveLeftDown", false);
			theanim.GetComponent<Animator> ().SetBool ("MoveRightDown", false);
			theanim.GetComponent<Animator> ().SetBool ("MoveRightUp", false);
			theanim.GetComponent<Animator> ().SetBool ("NotMoving", false);
			*/
		/* if (!(Input.GetKey ("up") || Input.GetKey ("down") || Input.GetKey ("left") || Input.GetKey ("right"))) {
			theanim.GetComponent<Animator> ().SetBool ("MoveUp", false);
			theanim.GetComponent<Animator> ().SetBool ("MoveDown", false);
			theanim.GetComponent<Animator> ().SetBool ("MoveLeft", false);
			theanim.GetComponent<Animator> ().SetBool ("MoveRight", false);
			theanim.GetComponent<Animator> ().SetBool ("MoveLeftUp", false);
			theanim.GetComponent<Animator> ().SetBool ("MoveLeftDown", false);
			theanim.GetComponent<Animator> ().SetBool ("MoveRightDown", false);
			theanim.GetComponent<Animator> ().SetBool ("MoveRightUp", false);
			// if (lastdirectionvar.thelastdirection == "down") {
				theanim.GetComponent<Animator> ().SetBool ("NotMoving", true);

			// }
		} */
		if( Input.GetKey("up") && Input.GetKey("left")){

			theanim.GetComponent<Animator> ().SetBool ("MoveUp", false);
			theanim.GetComponent<Animator> ().SetBool ("MoveDown", false);
			theanim.GetComponent<Animator> ().SetBool ("MoveLeft", false);
			theanim.GetComponent<Animator> ().SetBool ("MoveRight", false);
			theanim.GetComponent<Animator> ().SetBool ("MoveLeftUp", true);
			theanim.GetComponent<Animator> ().SetBool ("MoveLeftDown", false);
			theanim.GetComponent<Animator> ().SetBool ("MoveRightDown", false);
			theanim.GetComponent<Animator> ().SetBool ("MoveRightUp", false);
			theanim.GetComponent<Animator> ().SetBool ("NotMoving", false);
			character.transform.localPosition = new Vector3 (character.transform.localPosition.x, character.transform.localPosition.y + 0.05f * MoveSpeed, character.transform.localPosition.z);
			character.transform.localPosition = new Vector3 (character.transform.localPosition.x - 0.05f * MoveSpeed, character.transform.localPosition.y, character.transform.localPosition.z);

			}

		if( Input.GetKey("down") && Input.GetKey("left")){

			theanim.GetComponent<Animator> ().SetBool ("MoveUp", false);
			theanim.GetComponent<Animator> ().SetBool ("MoveDown", false);
			theanim.GetComponent<Animator> ().SetBool ("MoveLeft", false);
			theanim.GetComponent<Animator> ().SetBool ("MoveRight", false);
			theanim.GetComponent<Animator> ().SetBool ("MoveLeftUp", false);
			theanim.GetComponent<Animator> ().SetBool ("MoveLeftDown", true);
			theanim.GetComponent<Animator> ().SetBool ("MoveRightDown", false);
			theanim.GetComponent<Animator> ().SetBool ("MoveRightUp", false);
			theanim.GetComponent<Animator> ().SetBool ("NotMoving", false);
			character.transform.localPosition = new Vector3 (character.transform.localPosition.x, character.transform.localPosition.y - 0.05f * MoveSpeed, character.transform.localPosition.z);
			character.transform.localPosition = new Vector3 (character.transform.localPosition.x - 0.05f * MoveSpeed, character.transform.localPosition.y, character.transform.localPosition.z);

		}


		if( Input.GetKey("up") && Input.GetKey("right")){

			theanim.GetComponent<Animator> ().SetBool ("MoveUp", false);
			theanim.GetComponent<Animator> ().SetBool ("MoveDown", false);
			theanim.GetComponent<Animator> ().SetBool ("MoveLeft", false);
			theanim.GetComponent<Animator> ().SetBool ("MoveRight", false);
			theanim.GetComponent<Animator> ().SetBool ("MoveLeftUp", false);
			theanim.GetComponent<Animator> ().SetBool ("MoveLeftDown", false);
			theanim.GetComponent<Animator> ().SetBool ("MoveRightDown", false);
			theanim.GetComponent<Animator> ().SetBool ("MoveRightUp", true);
			theanim.GetComponent<Animator> ().SetBool ("NotMoving", false);
			character.transform.localPosition = new Vector3 (character.transform.localPosition.x, character.transform.localPosition.y + 0.05f * MoveSpeed, character.transform.localPosition.z);
			character.transform.localPosition = new Vector3 (character.transform.localPosition.x + 0.05f * MoveSpeed, character.transform.localPosition.y, character.transform.localPosition.z);

		}
		if( Input.GetKey("down") && Input.GetKey("right")){

			theanim.GetComponent<Animator> ().SetBool ("MoveUp", false);
			theanim.GetComponent<Animator> ().SetBool ("MoveDown", false);
			theanim.GetComponent<Animator> ().SetBool ("MoveLeft", false);
			theanim.GetComponent<Animator> ().SetBool ("MoveRight", false);
			theanim.GetComponent<Animator> ().SetBool ("MoveLeftUp", false);
			theanim.GetComponent<Animator> ().SetBool ("MoveLeftDown", false);
			theanim.GetComponent<Animator> ().SetBool ("MoveRightDown", true);
			theanim.GetComponent<Animator> ().SetBool ("MoveRightUp", false);
			theanim.GetComponent<Animator> ().SetBool ("NotMoving", false);
			character.transform.localPosition = new Vector3 (character.transform.localPosition.x, character.transform.localPosition.y - 0.05f * MoveSpeed, character.transform.localPosition.z);
			character.transform.localPosition = new Vector3 (character.transform.localPosition.x + 0.05f * MoveSpeed, character.transform.localPosition.y, character.transform.localPosition.z);

		}

		if ( !(Input.GetKey("up") && Input.GetKey("right")) && !(Input.GetKey("up") && Input.GetKey("left")) && !(Input.GetKey("down") && Input.GetKey("right")) && !(Input.GetKey("down") && Input.GetKey("left")) ) {
			if (Input.GetKey ("up")) {
				theanim.GetComponent<Animator> ().SetBool ("MoveUp", true);
				theanim.GetComponent<Animator> ().SetBool ("MoveDown", false);
				theanim.GetComponent<Animator> ().SetBool ("MoveLeft", false);
				theanim.GetComponent<Animator> ().SetBool ("MoveRight", false);
				theanim.GetComponent<Animator> ().SetBool ("MoveLeftUp", false);
				theanim.GetComponent<Animator> ().SetBool ("MoveLeftDown", false);
				theanim.GetComponent<Animator> ().SetBool ("MoveRightDown", false);
				theanim.GetComponent<Animator> ().SetBool ("MoveRightUp", false);
				theanim.GetComponent<Animator> ().SetBool ("NotMoving", false);
				character.transform.localPosition = new Vector3 (character.transform.localPosition.x, character.transform.localPosition.y + 0.05f * MoveSpeed, character.transform.localPosition.z);
			}
			if (Input.GetKey ("down")) {
				theanim.GetComponent<Animator> ().SetBool ("MoveUp", false);
				theanim.GetComponent<Animator> ().SetBool ("MoveDown", true);
				theanim.GetComponent<Animator> ().SetBool ("MoveLeft", false);
				theanim.GetComponent<Animator> ().SetBool ("MoveRight", false);
				theanim.GetComponent<Animator> ().SetBool ("MoveLeftUp", false);
				theanim.GetComponent<Animator> ().SetBool ("MoveLeftDown", false);
				theanim.GetComponent<Animator> ().SetBool ("MoveRightDown", false);
				theanim.GetComponent<Animator> ().SetBool ("MoveRightUp", false);
				theanim.GetComponent<Animator> ().SetBool ("NotMoving", false);
				character.transform.localPosition = new Vector3 (character.transform.localPosition.x, character.transform.localPosition.y - 0.05f * MoveSpeed, character.transform.localPosition.z);
			}
			if (Input.GetKey ("left")) {
				theanim.GetComponent<Animator> ().SetBool ("MoveUp", false);
				theanim.GetComponent<Animator> ().SetBool ("MoveDown", false);
				theanim.GetComponent<Animator> ().SetBool ("MoveLeft", true);
				theanim.GetComponent<Animator> ().SetBool ("MoveRight", false);
				theanim.GetComponent<Animator> ().SetBool ("MoveLeftUp", false);
				theanim.GetComponent<Animator> ().SetBool ("MoveLeftDown", false);
				theanim.GetComponent<Animator> ().SetBool ("MoveRightDown", false);
				theanim.GetComponent<Animator> ().SetBool ("MoveRightUp", false);
				theanim.GetComponent<Animator> ().SetBool ("NotMoving", false);
				character.transform.localPosition = new Vector3 (character.transform.localPosition.x - 0.05f * MoveSpeed, character.transform.localPosition.y, character.transform.localPosition.z);
			}
			if (Input.GetKey ("right")) {
				theanim.GetComponent<Animator> ().SetBool ("MoveUp", false);
				theanim.GetComponent<Animator> ().SetBool ("MoveDown", false);
				theanim.GetComponent<Animator> ().SetBool ("MoveLeft", false);
				theanim.GetComponent<Animator> ().SetBool ("MoveRight", true);
				theanim.GetComponent<Animator> ().SetBool ("MoveLeftUp", false);
				theanim.GetComponent<Animator> ().SetBool ("MoveLeftDown", false);
				theanim.GetComponent<Animator> ().SetBool ("MoveRightDown", false);
				theanim.GetComponent<Animator> ().SetBool ("MoveRightUp", false);
				theanim.GetComponent<Animator> ().SetBool ("NotMoving", false);
				character.transform.localPosition = new Vector3 (character.transform.localPosition.x + 0.05f * MoveSpeed, character.transform.localPosition.y, character.transform.localPosition.z);
			}

		}

		/* if (Input.GetAxisRaw ("Horizontal") > 0.5f || Input.GetAxisRaw("Horizontal") < -0.5f) {
			character.transform.Translate (new Vector3 (Input.GetAxisRaw ("Horizontal") * MoveSpeed * Time.deltaTime, 0f, 0f));
		}
		if (Input.GetAxisRaw ("Vertical") > 0.5f || Input.GetAxisRaw ("Vertical") < -0.5f ) {
			character.transform.Translate (new Vector3 (0f,Input.GetAxisRaw ("Vertical") * MoveSpeed * Time.deltaTime,0f));
		} 

		anim.GetComponent<Animator> ().SetFloat ("MoveX", Input.GetAxisRaw ("Horizontal"));
		anim.GetComponent<Animator> ().SetFloat ("MoveY", Input.GetAxisRaw ("Vertical")); */

	
	}
}

public class LastDirection {
		public string thelastdirection;
}

This is code for your character :

void Update () {
        float MoveSpeed = 1f;      
              
          // change it to your object name that work as character
          
           string charactername = "taoist";
       
        // change the string to your parameter     
                   
        string characterfrontwalk = "taoist_front_walk";
        string characterbackwalk = "taoist_back_walk";
        string characterleftwalk = "taoist_left_walk";
        string characterrightwalk = "taoist_right_walk";
        /* if you want corner walk 
        string characterrightfrontwalk = "taoist_right_front_walk";
        string characterrightbackwalk = "taoist_right_back_walk";
        string characterleftfrontwalk = "taoist_left_front_walk";
        string characterleftbackwalk = "taoist_left_back_walk";
        // to use this corner setting , you must initialize yourself 
        
        */
		GameObject character = GameObject.Find(charactername);
		GameObject theanim = GameObject.Find(charactername);
		theanim.GetComponent<Animator> ();
			theanim.GetComponent<Animator> ().SetBool (characterfrontwalk, true);
			theanim.GetComponent<Animator> ().SetBool (characterbackwalk, false);
			theanim.GetComponent<Animator> ().SetBool (characterleftwalk, false);
			theanim.GetComponent<Animator> ().SetBool (characterrightwalk, false);
			character.transform.localPosition = new Vector3 (character.transform.localPosition.x, character.transform.localPosition.y + 0.05f * MoveSpeed, character.transform.localPosition.z);
			character.transform.localPosition = new Vector3 (character.transform.localPosition.x - 0.05f * MoveSpeed, character.transform.localPosition.y, character.transform.localPosition.z);

			}

		/* 
        
        // THIS CODE IS FOR CORNER MOVE , UN-MODIFIED , because im too tired to modify it sorry , you need to think yourself ;) 
        
        if( Input.GetKey("down") && Input.GetKey("left")){

			theanim.GetComponent<Animator> ().SetBool ("MoveUp", false);
			theanim.GetComponent<Animator> ().SetBool ("MoveDown", false);
			theanim.GetComponent<Animator> ().SetBool ("MoveLeft", false);
			theanim.GetComponent<Animator> ().SetBool ("MoveRight", false);
			theanim.GetComponent<Animator> ().SetBool ("MoveLeftUp", false);
			theanim.GetComponent<Animator> ().SetBool ("MoveLeftDown", true);
			theanim.GetComponent<Animator> ().SetBool ("MoveRightDown", false);
			theanim.GetComponent<Animator> ().SetBool ("MoveRightUp", false);
			theanim.GetComponent<Animator> ().SetBool ("NotMoving", false);
			character.transform.localPosition = new Vector3 (character.transform.localPosition.x, character.transform.localPosition.y - 0.05f * MoveSpeed, character.transform.localPosition.z);
			character.transform.localPosition = new Vector3 (character.transform.localPosition.x - 0.05f * MoveSpeed, character.transform.localPosition.y, character.transform.localPosition.z);

		}


		if( Input.GetKey("up") && Input.GetKey("right")){

			theanim.GetComponent<Animator> ().SetBool ("MoveUp", false);
			theanim.GetComponent<Animator> ().SetBool ("MoveDown", false);
			theanim.GetComponent<Animator> ().SetBool ("MoveLeft", false);
			theanim.GetComponent<Animator> ().SetBool ("MoveRight", false);
			theanim.GetComponent<Animator> ().SetBool ("MoveLeftUp", false);
			theanim.GetComponent<Animator> ().SetBool ("MoveLeftDown", false);
			theanim.GetComponent<Animator> ().SetBool ("MoveRightDown", false);
			theanim.GetComponent<Animator> ().SetBool ("MoveRightUp", true);
			theanim.GetComponent<Animator> ().SetBool ("NotMoving", false);
			character.transform.localPosition = new Vector3 (character.transform.localPosition.x, character.transform.localPosition.y + 0.05f * MoveSpeed, character.transform.localPosition.z);
			character.transform.localPosition = new Vector3 (character.transform.localPosition.x + 0.05f * MoveSpeed, character.transform.localPosition.y, character.transform.localPosition.z);

		}
		if( Input.GetKey("down") && Input.GetKey("right")){

			theanim.GetComponent<Animator> ().SetBool ("MoveUp", false);
			theanim.GetComponent<Animator> ().SetBool ("MoveDown", false);
			theanim.GetComponent<Animator> ().SetBool ("MoveLeft", false);
			theanim.GetComponent<Animator> ().SetBool ("MoveRight", false);
			theanim.GetComponent<Animator> ().SetBool ("MoveLeftUp", false);
			theanim.GetComponent<Animator> ().SetBool ("MoveLeftDown", false);
			theanim.GetComponent<Animator> ().SetBool ("MoveRightDown", true);
			theanim.GetComponent<Animator> ().SetBool ("MoveRightUp", false);
			theanim.GetComponent<Animator> ().SetBool ("NotMoving", false);
			character.transform.localPosition = new Vector3 (character.transform.localPosition.x, character.transform.localPosition.y - 0.05f * MoveSpeed, character.transform.localPosition.z);
			character.transform.localPosition = new Vector3 (character.transform.localPosition.x + 0.05f * MoveSpeed, character.transform.localPosition.y, character.transform.localPosition.z);

		} 
        // Un-MODIFIED corner setting , initialize yourself 
            */
		if ( !(Input.GetKey("up") && Input.GetKey("right")) && !(Input.GetKey("up") && Input.GetKey("left")) && !(Input.GetKey("down") && Input.GetKey("right")) && !(Input.GetKey("down") && Input.GetKey("left")) ) {
			if (Input.GetKey ("up")) {
				theanim.GetComponent<Animator> ().SetBool (characterfrontwalk, true);
				theanim.GetComponent<Animator> ().SetBool (characterbackwalk, false);
				theanim.GetComponent<Animator> ().SetBool (characterleftwalk, false);
				theanim.GetComponent<Animator> ().SetBool (characterrightwalk, false);
				
				character.transform.localPosition = new Vector3 (character.transform.localPosition.x, character.transform.localPosition.y + 0.05f * MoveSpeed, character.transform.localPosition.z);
			} 
			if (Input.GetKey ("down")) {
				theanim.GetComponent<Animator> ().SetBool (characterfrontwalk, false);
				theanim.GetComponent<Animator> ().SetBool (characterbackwalk, true);
				theanim.GetComponent<Animator> ().SetBool (characterleftwalk, false);
				theanim.GetComponent<Animator> ().SetBool (characterrightwalk, false);
				character.transform.localPosition = new Vector3 (character.transform.localPosition.x, character.transform.localPosition.y - 0.05f * MoveSpeed, character.transform.localPosition.z);
			}
			if (Input.GetKey ("left")) {
				theanim.GetComponent<Animator> ().SetBool (characterfrontwalk, false);
				theanim.GetComponent<Animator> ().SetBool (characterbackwalk, false);
				theanim.GetComponent<Animator> ().SetBool (characterleftwalk, true);
				theanim.GetComponent<Animator> ().SetBool (characterrightwalk, false);
				character.transform.localPosition = new Vector3 (character.transform.localPosition.x - 0.05f * MoveSpeed, character.transform.localPosition.y, character.transform.localPosition.z);
			}
			if (Input.GetKey ("right")) {
				theanim.GetComponent<Animator> ().SetBool (characterfrontwalk, false);
				theanim.GetComponent<Animator> ().SetBool (characterbackwalk, false);
				theanim.GetComponent<Animator> ().SetBool (characterleftwalk, false);
				theanim.GetComponent<Animator> ().SetBool (characterrightwalk, true);
				character.transform.localPosition = new Vector3 (character.transform.localPosition.x + 0.05f * MoveSpeed, character.transform.localPosition.y, character.transform.localPosition.z);
			}

		}


	
	}

\$\endgroup\$
  • \$\begingroup\$ Seriously thanks so much for your effort and you even made a Youtube video to help me out. Very touching. I'm still trying my best to understand the code. I upload a picture in the question above as I can't upload it at this comment. Is this the correct set up of mecanim state in animator? I want to move my sprite in four direction: up, down, left, right. \$\endgroup\$ – Philip Chow Nov 27 '16 at 14:54
  • \$\begingroup\$ its right but is the conditions right ? go click the transition and look at inspector \$\endgroup\$ – user6668201 Nov 27 '16 at 15:04
  • \$\begingroup\$ @ user6668201 For the condition, I set it as character_front_walk(state) > character_back_walk(state) using condition: backwalk = true ::::::::::::::::::::::::: character_back_walk(state) > character_front_walk(state) using condition: frontwalk = true \$\endgroup\$ – Philip Chow Nov 27 '16 at 15:14
  • \$\begingroup\$ you need more condition , example like character_front_walk > character_back_walk , use backwalk = true and frontwalk = false not only backwalk = true \$\endgroup\$ – user6668201 Nov 27 '16 at 15:20
  • \$\begingroup\$ can you tell me all the state and the object name that used as "character" ? i can create the script for you \$\endgroup\$ – user6668201 Nov 27 '16 at 15:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.