Jump to content
  • Advertisement
Sign in to follow this  
Howgyn

Unity Fail to change multiple sprite mecanim state using C# scripts

This topic is 667 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Bottom is the attachment of my mecanim state 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;
}
Edited by Howgyn

Share this post


Link to post
Share on other sites
Advertisement
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!