- Home /
Making a 2d sprite play its animation at given waypoint.
Hello, everyone. I'm still quite a beginner in coding.
I'm trying to make 2.5D like game as a practice project.
I'm trying to get my 2d sprite character idle animation to play after its moving animation done reaching to the given way point.
This is the code I've done so far:
public Transform[] points;
private int destPoint;
private NavMeshAgent agent;
private Animator anim;
bool _isWalkingUp = false;
const int STATE_IDLEFRONT = 0;
const int STATE_WALKFRONT = 1;
void moveThere()
{
if (_isWalkingUp == false) {
agent.destination = points [0].position;
changeState (STATE_WALKFRONT);
_isWalkingUp = true;
} else if (_isWalkingUp == true) {
changeState(STATE_IDLEFRONT);
}
}
void changeState(int state){
if (_currentAnimationState == state)
return;
switch (state) {
case STATE_IDLEFRONT:
anim.SetInteger ("state" , STATE_IDLEFRONT);
break;
case STATE_WALKFRONT:
anim.SetInteger ("state" , STATE_WALKFRONT);
break;
}
_currentAnimationState = state;
}
Its on this part here where my character does its front move animation and then reaches its waypoint but my idle animation doesn't play it from there. Even the debug.log there is a no show. Can anyone help me?
if (_isWalkingUp == false)
{
agent.destination = points [0].position;
changeState (STATE_WALKFRONT);
Debug.Log ("Walking There");
_isWalkingUp = true;
} else if (_isWalkingUp == true) {
Debug.Log("Meow");
changeState(STATE_IDLEFRONT);
}
}
Share an image of the animator and the transitions you used, please
Answer by DiegoSLTS · Oct 23, 2015 at 05:33 PM
I think you're missing a like to set _IsWalkingUp back to false.
Debug.Log("Meow");
changeState(STATE_IDLEFRONT);
_IsWalkingOn = false;
Sadly, it didn't work. It seems like my else if statement isn't being read at all. Which I'm very confused about now.
How is the "moveThere" method called? Share the code or explain a bit more how it's used. Also, are you sure _isWalkingUp is not being changed somwhere else?
Its on my void awake. I forgot to add this part. and No, the _isWalkingUp is only called at moveThere() function.
void Awake() {
anim = this.GetComponent<Animator>();
agent = GetComponent<Nav$$anonymous$$eshAgent> ();
agent.autoBraking = true;
moveThere ();
}