Sign in to follow this  
Howgyn

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

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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this  

  • Announcements

  • Forum Statistics

    • Total Topics
      628349
    • Total Posts
      2982210
  • Similar Content

    • By koto
      Hello,

      I'm developing a roguelike game. Currently it's in a playable state and I think it's a high time to show it and hopefully get some feedback.

      The game has following features:
      - turn based
      - procedurally generated levels
      - tons of loot
      - spells system
      - trading system
      - some quests
      - some limited crafting
      - door/key/lever mechanics
      - quite a lot of different (look/behaviour) monsters

      Trailer:
      Demo: http://kotogames.com/wp-content/uploads/2017/11/OuaD_demo.zip
      Regards,
      Tom
       
    • By Afambus
      Hello,
      I have a full experienced team that looking for a composer and another 3d designer, im expecting as much from you there are other people in this server that can help you we have a determined team, we have done many things in our game there is more information in the discord about everyone and there skills there region and about the game, if you join the discord pm asap
      https://discord.gg/b2teN3m
    • By BAG Labs
      Mobile SoS

      Platform: Android
      Genre: Board
      Link: Google Play
       
      This games sharpen memory and test your strategies to place S-O-S pattern within time limit and serve 3 difficulties as Easy, Normal, and Hard.

      Goals of the game is to put S-O-S words in patterns (Horizontal, Vertical, and Diagonal) alternately with enemy.
       
      Features:
      Single Player Multiplayer Achievements Leaderboards  
       
      Screenshot:

       

       

       
      Link: Google Play
       
      Please help us improve this game with review
    • By abarnes
      Hello!
      I am a game development student in my second year of a three year program and I would like to start building my portfolio. I was thinking of creating some games to show what I can do to potential employers since I wont have any work related experience when I graduate. But as I'm sure you all know there are tons of ways to approach developing/designing a game and I'm curious if anyone had any insight as to any "standards" that come with this? Is it okay to use game engines like Unity, Unreal, Game Maker etc? Or would it be better to make a game from scratch to better show case your skills? Any and all advice will be greatly appreciated!
    • By Hilster
      Hello 2D Artists,
      I've started making a 2D Puzzle Adventure game for mobile and I'm looking for someone who would want in on creating assets for the game. The core of the programming is pretty much complete, you can walk within the grid laid out and push boxes, when there is an object on top of a pressure pad it will activate the linked objects or if there is one object with multiple linked pressure pads it requires you to activate all points for the object to become active. 

      The level iteration for the game is quick and simple, a Photoshop file that is made of individual pixels that represents objects is put into the game and it creates the level out of those pixels with the assigned objects.
      The objects that need sprites created so far is the character, box, pressure pad, door, trap door, the walls, the stairs and the tiled background.
      I intend to add more objects so the amount I'd like to add will be extended.
      My motivations for posting here is to have something that looks nice to be able to display on my portfolio, so if you're looking for a working game that you can place your art into and improve the look of your portfolio then we're in business.
      Please reply with a few past examples of your art below and I'll be in touch!
  • Popular Now