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

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

## 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

• 12
• 10
• 11
• 18
• 13
• ### Similar Content

• Hi fellow game devs,
First, I would like to apologize for the wall of text.
As you may notice I have been digging in vehicle simulation for some times now through my clutch question posts. And thanks to the generous help of you guys, especially @CombatWombat I have finished my clutch model (Really CombatWombat you deserve much more than a post upvote, I would buy you a drink if I could ha ha).
Now the final piece in my vehicle physic model is the differential. For now I have an open-differential model working quite well by just outputting torque 50-50 to left and right wheel. Now I would like to implement a Limited Slip Differential. I have very limited knowledge about LSD, and what I know about LSD is through readings on racer.nl documentation, watching Youtube videos, and playing around with games like Assetto Corsa and Project Cars. So this is what I understand so far:
- The LSD acts like an open-diff when there is no torque from engine applied to the input shaft of the diff. However, in clutch-type LSD there is still an amount of binding between the left and right wheel due to preload spring.
- When there is torque to the input shaft (on power and off power in 2 ways LSD), in ramp LSD, the ramp will push the clutch patch together, creating binding force. The amount of binding force depends on the amount of clutch patch and ramp angle, so the diff will not completely locked up and there is still difference in wheel speed between left and right wheel, but when the locking force is enough the diff will lock.
- There also something I'm not sure is the amount of torque ratio based on road resistance torque (rolling resistance I guess)., but since I cannot extract rolling resistance from the tire model I'm using (Unity wheelCollider), I think I would not use this approach. Instead I'm going to use the speed difference in left and right wheel, similar to torsen diff. Below is my rough model with the clutch type LSD:
speedDiff = leftWheelSpeed - rightWheelSpeed; //torque to differential input shaft. //first treat the diff as an open diff with equal torque to both wheels inputTorque = gearBoxTorque * 0.5f; //then modify torque to each wheel based on wheel speed difference //the difference in torque depends on speed difference, throttleInput (on/off power) //amount of locking force wanted at different amount of speed difference, //and preload force //torque to left wheel leftWheelTorque = inputTorque - (speedDiff * preLoadForce + lockingForce * throttleInput); //torque to right wheel rightWheelTorque = inputTorque + (speedDiff * preLoadForce + lockingForce * throttleInput); I'm putting throttle input in because from what I've read the amount of locking also depends on the amount of throttle input (harder throttle -> higher  torque input -> stronger locking). The model is nowhere near good, so please jump in and correct me.
Also I have a few questions:
- In torsen/geared LSD, is it correct that the diff actually never lock but only split torque based on bias ratio, which also based on speed difference between wheels? And does the bias only happen when the speed difference reaches the ratio (say 2:1 or 3:1) and below that it will act like an open diff, which basically like an open diff with an if statement to switch state?
- Is it correct that the amount of locking force in clutch LSD depends on amount of input torque? If so, what is the threshold of the input torque to "activate" the diff (start splitting torque)? How can I get the amount of torque bias ratio (in wheelTorque = inputTorque * biasRatio) based on the speed difference or rolling resistance at wheel?
- Is the speed at the input shaft of the diff always equals to the average speed of 2 wheels ie (left + right) / 2?
• By Estra
Memory Trees is a PC game and Life+Farming simulation game. Harvest Moon and Rune Factory , the game will be quite big. I believe that this will take a long time to finish
Looking for
Programmer
1 experience using Unity/C++
2 have a portfolio of Programmer
3 like RPG game ( Rune rune factory / zelda series / FF series )
4 Have responsibility + Time Management
and friendly easy working with others Programmer willing to use Skype for communication with team please E-mail me if you're interested
Split %: Revenue share. We can discuss. Fully Funded servers and contents
and friendly easy working with others willing to use Skype for communication with team please E-mail me if you're interested
we can talk more detail in Estherfanworld@gmail.com Don't comment here
Thank you so much for reading
Memory Trees : forget me not

Thank you so much for reading
Ps.Please make sure that you have unity skill and Have responsibility + Time Management,
because If not it will waste time not one but both of us

• We've now started desinging the 3rd level of "Something Ate My Alien".
This world is a gas planet, and all sorts of mayhem will be getting in our aliens way!
#screenshotsaturday

• Kepuh's Island is Multiplayer 3D Survival Game where you survive on the Kepuh's Islands, confronting challenges that are not only other players but also bosses, and even the environment itself.
We have a lowpoly faster battle-royale idea, where about 12 players on the map fighting for survival! Also adding some more things into that style such as bosses around the map giving you abilities and much more such as vehicles, weapons, skins, etc...
Now we are on cartase which is a crowdfunding online which purpose is to raise funds for the development of the game. Come and be part of this development.