- Home /
Having some issues with my Animator Controller.
So I'm working on an Over world for my Turn based Role Playing Game, and it's going kind of well, but I've hit a roadblock at the animations phase. So I have 4 different animations, a frontIdle, backIdle, frontWalk, and backWalk. Here's my script for the animator Controller. The issue i'm having is that it just won't connect I guess? I have the script linked to my player object, along with the animator Controller, but whenever I test it, he just kind of floats around in his idle animation, which is the sprite renderer sprite as well. please keep in mind this is my first submission to the forums as well as my first attempt at a game in general, so some soft criticisms would be appreciated!
public class PlayerControllerOverworld : MonoBehaviour
{
public float walkSpeed = 1; // player left right walk speed
private bool _isFront = true; //check to see if player is facing front
Animator animator;
//some flags to check when certain animations are playing
//bool _isPlaying_walk = false;
//animation states - the values in the animator conditions
const int STATE_IDLE = 0;
const int STATE_WALK = 1;
const int STATE_BACKWALK = 2;
const int STATE_BACKIDLE = 3;
const int FRONT = 0;
const int BACK = 1;
string _currentDirection = "right";
int _currentAnimationState = STATE_IDLE;
int _currentFrontOrBack = FRONT;
// Use this for initialization
void Start ()
{
//define the animator attached to the player
animator = this.GetComponent<Animator>();
}
// FixedUpdate is used insead of Update to better handle the physics based jump
void FixedUpdate()
{
//Check for keyboard input
if (Input.GetKey ("right"))
{
changeDirection ("right");
transform.Translate(Vector3.right * walkSpeed * Time.deltaTime * 3);
if(_isFront == true)
changeState(STATE_WALK);
else if(_isFront == false)
changeState(STATE_BACKWALK);
}
else if (Input.GetKey ("left"))
{
changeDirection ("left");
transform.Translate(Vector3.right * walkSpeed * Time.deltaTime * 3);
if(_isFront == true)
changeState(STATE_WALK);
else if(_isFront == false)
changeState(STATE_BACKWALK);
}
else if (Input.GetKey ("up"))
{
transform.Translate(Vector3.up * walkSpeed * Time.deltaTime * 3);
_isFront = false;
if(_isFront == false)
changeState(STATE_BACKWALK);
}
else if (Input.GetKey ("down"))
{
transform.Translate(Vector3.down * walkSpeed * Time.deltaTime * 3);
_isFront = true;
if(_isFront == true)
changeState(STATE_WALK);
}
else
{
if(_isFront == true)
changeState(STATE_IDLE);
else if (_isFront == false)
changeState(STATE_BACKIDLE);
}
}
//--------------------------------------
// Change the players animation state
//--------------------------------------
void changeState(int state){
if (_currentAnimationState == state)
return;
switch (state) {
case STATE_WALK:
animator.SetInteger ("state", STATE_WALK);
break;
case STATE_IDLE:
animator.SetInteger ("state", STATE_IDLE);
break;
case STATE_BACKWALK:
animator.SetInteger ("state", STATE_BACKWALK);
break;
case STATE_BACKIDLE:
animator.SetInteger ("state", STATE_BACKIDLE);
break;
}
_currentAnimationState = state;
}
void changeBackOrFront(int backOrFront){
if (_currentFrontOrBack == backOrFront)
return;
switch (backOrFront) {
case STATE_WALK:
animator.SetInteger ("backOrFront", FRONT);
break;
case STATE_IDLE:
animator.SetInteger ("backOrFront", FRONT);
break;
case STATE_BACKWALK:
animator.SetInteger ("backOrFront", BACK);
break;
case STATE_BACKIDLE:
animator.SetInteger ("backOrFront", BACK);
break;
}
_currentFrontOrBack = backOrFront;
}
//--------------------------------------
// Check if player has collided with the floor
//--------------------------------------
void OnCollisionEnter2D(Collision2D coll)
{
if (coll.gameObject.name == "Floor")
{
_isFront = true;
changeState(STATE_IDLE);
}
if (coll.gameObject.name == "WallLeft")
{
_isFront = true;
changeState(STATE_IDLE);
}
if (coll.gameObject.name == "WallRight")
{
_isFront = true;
changeState(STATE_IDLE);
}
if (coll.gameObject.name == "Roof")
{
_isFront = true;
changeState(STATE_IDLE);
}
}
//--------------------------------------
// Flip player sprite for left/right walking
//--------------------------------------
void changeDirection(string direction)
{
if (_currentDirection != direction)
{
if (direction == "right")
{
transform.Rotate (0, 180, 0);
_currentDirection = "right";
}
else if (direction == "left")
{
transform.Rotate (0, -180, 0);
_currentDirection = "left";
}
}
}
}
If you star the game without $$anonymous$$aximizeOnPlay set you can also highlight your player in the hierarchy and then view the Animator screen whilst playing the game.
Do this and see if your integers are being set correctly. Then report back.
yup, it's supposedly playing the animations. the animator controller is switching between them, but nothing is changing in the actual game.
Never$$anonymous$$d, thanks a bunch though! The problem was in my actual animations, not the scripting or animator controller.
Glad you got it sorted @$$anonymous$$atty$$anonymous$$idori
Your answer
Follow this Question
Related Questions
why I have to anim.getComponent in update() function when I had done in Start () function 2 Answers
Is there a new retargetting system for the animation in 5.5? 1 Answer
2D Animation does not start 1 Answer
StateMachineBehaviour Issue when swapping runtimeAnimatorController 0 Answers
Help with animator controllers 1 Answer