- Home /
When walking, my character will not change sprites.
As the title explains, I have a game object with three sprite animations in it:an idle, walking left, and walking right animation. When play the game, I can move left and right fine, but the sprites are not functioning correctly. The walking animations play at the same time even though I am not pressing any keys and the idle animation does not play at all. Here is my script:
static var speed : int = 1;
public var CLEFT : GameObject;
public var CRIGHT : GameObject;
public var CIDLE : GameObject;
function Update () {
if (Input.GetKey (KeyCode.LeftArrow)) transform.Translate (Vector3(-1,0,0) * Time.deltaTime*speed);
{
CLEFT.gameObject.active = true;
CIDLE.gameObject.active = false;
}
if (Input.GetKey (KeyCode.RightArrow)) transform.Translate (Vector3(1,0,0) * Time.deltaTime*speed);
{
CRIGHT.gameObject.active = true;
CIDLE.gameObject.active = false;
}
}
Any feedback would be greatly appreciated!
Did you try to add an else statement that starts the idle animation when no button gets pressed?
I tried adding an else statement but now it is pulling multiple errors:
#pragma strict
static var speed : int = 1;
public var walking_left : GameObject;
public var walking_right : GameObject;
function Update()
{
if (Input.Get$$anonymous$$ey ($$anonymous$$eyCode.LeftArrow))transform.Translate (Vector3(-1,0,0) * Time.deltaTime*speed);
{
walking_left.gameObject.SetActive (true);
}
else{
walking_left.gameObject.SetActive (false);
}
if (Input.Gey$$anonymous$$ey($$anonymous$$eyCode.RightArrow))transform.Translate (Vector3(1,0,0) * Time.deltaTime*speed);
{
walking_right.gameObject.SetActive (true);
}
else{
walking_right.gameObject.SetActive (false);
}
}
It is not the most elegant way but try this:
function Update()
{
if (Input.Get$$anonymous$$ey($$anonymous$$eyCode.LeftArrow)) {
transform.Translate (Vector3(-1,0,0) * Time.deltaTime*speed);
walking_left.gameObject.SetActive (true);
//code to turn off the idle animation goes here
}
else if (Input.Get$$anonymous$$ey($$anonymous$$eyCode.RightArrow)) {
transform.Translate (Vector3(1,0,0) * Time.deltaTime*speed);
walking_right.gameObject.SetActive (true);
//code to turn off the idle animation goes here
}
else{
walking_left.gameObject.SetActive (false);
walking_right.gameObject.SetActive (false);
//code to activate your idle animation goes here
}
}
Your brakets were out of place and there was a spelling error with "Input.Gey$$anonymous$$ey". I also added else if statements. You only want one of the 3 states (w-left;w-right and idle) to trigger so they should be connected.
Answer by maccabbe · Apr 20, 2015 at 04:44 PM
There are many problems with your original code. The most worrysome is that you are trying to use both the line of text and block of code following your if statement. However the following code
if(condition) // line of code;
{
// block of code;
}
is the same as
if(condition){
// line of code
}
if(true){
// block of code
}
You should instead combine the original line of code and block of code into a new block of code
if(condition){
// line of code
// block of code
}
In addition you should use if/elseif/else statments so you can set the idle animation. Also since CLEFT, CRIGHT, and CIDLE, are gameObjects this is redundant to get their gameObject. You should also use SetActive instead of active if you want to change a gameObject's activity. Finally, it is easier to read Vector3.left and Vector3.right than Vector3(1, 0, 0) and Vector3(-1, 0, 0). Applying all these changes results in the following
static var speed : int = 1;
public var CLEFT : GameObject;
public var CRIGHT : GameObject;
public var CIDLE : GameObject;
function Update () {
if (Input.GetKey (KeyCode.LeftArrow))
{
transform.Translate (Vector3.left * Time.deltaTime*speed);
CRIGHT.SetActive(true);
CLEFT.SetActive(false);
CIDLE.SetActive(false);
}
else if (Input.GetKey (KeyCode.RightArrow))
{
transform.Translate (Vector3.right * Time.deltaTime*speed);
CRIGHT.SetActive(false);
CLEFT.SetActive(true);
CIDLE.SetActive(false);
}
else{
CRIGHT.SetActive(false);
CLEFT.SetActive(false);
CIDLE.SetActive(true);
}
}
http://unity3d.com/learn/tutorials/modules/beginner/scripting/if-statements
Okay I understand my problem. Thank you everyone for resolving my issue, I will make sure to handle my if/else statements better in the future.
Your answer
Follow this Question
Related Questions
Character doesen't collide 3 Answers
character turning where he looks 0 Answers
I want to play an Audio Sound for a character while running. 0 Answers