Hi! I changed the code and I'm now using a trigger, so when the character get close to the grass (if the grass was clicked), the character stops and start the animation of "prunning". But the animation get stopped in the first frame. I would like if you make me see the mistake I'm doing, and thanks for all.
PDD: I think that the State is not changing to "Prunning" or it changes but in the same frame update is changing to "Idle"
TP_Controller: when I handle the inputs
public void Stop()
{
if(targetDirection.x > 0)
targetPosition = new Vector2(CharacterController.transform.position.x + (0.000002f), targetPosition.y);
else
targetPosition = new Vector2(CharacterController.transform.position.x - (0.000002f), targetPosition.y);
}//Stop()
public void HandleActionInput()
{
// Actions for Objects Clicked
if(Input.GetMouseButtonDown(0))
{
var objClicked = GetObjectClicked();
if(objClicked != null)
{
// If the object clicked is the ground or the grass, the character will move to the place clicked
if(objClicked.CompareTag("ground") ||
objClicked.CompareTag("grass"))
{
SetTargetToWalk();
// If the object clicked is the grass, we execute prune
if(objClicked.CompareTag("grass"))
Prune();
}
}
else
Debug.Log("Is null");
}
}//HandleActionInput()
void Prune()
{
TP_Animator.Instance.currentAction = "prunning";
}//Prune()
Grass: Script attached to the grass
void OnTriggerEnter(Collider other)
{
if(other.CompareTag("Player"))
{
TP_Animator tpa = (TP_Animator)other.GetComponent("TP_Animator");
if(tpa.currentAction == "prunning")
{
TP_Controller tpc = (TP_Controller) other.GetComponent("TP_Controller");
tpc.Stop();
tpa.Prune();
//tpa.State = TP_Animator.CharacterState.Pruning;
//StartCoroutine(tpa.ProcessCurrentState());
}
}
}//OnTriggerEnter()
TP_Animator: where the animations are executed
public enum CharacterState
{
WalkingLeft, WalkingRight, Running, Idle, Falling, Jumping, Dead, Hitting, Pruning
}
public enum Direction
{
Stationary, Left, Right
}
public CharacterState State;
public Direction MoveDirection;
public string currentAction = "";
........
void DetermineCurrentDirection()
{
if(TP_Controller.Instance.targetDirection.x > 0)
{
MoveDirection = Direction.Right;
}
else if(TP_Controller.Instance.targetDirection.x < 0)
MoveDirection = Direction.Left;
else if(TP_Controller.Instance.targetDirection.x == 0)
MoveDirection = Direction.Stationary;
}//DetermineCurrentDirection()
void DetermineCurrentState()
{
if(State == CharacterState.Dead)
return;
if(!TP_Controller.CharacterController.isGrounded)
Debug.Log("It should be falling");
if(State != CharacterState.Dead &&
State != CharacterState.Jumping &&
State != CharacterState.Falling)
{
switch(MoveDirection)
{
case Direction.Stationary:
State = CharacterState.Idle;
break;
case Direction.Right:
State = CharacterState.WalkingRight;
break;
case Direction.Left:
State = CharacterState.WalkingLeft;
break;
}
}
}//DetermineCurrentState()
public IEnumerator ProcessCurrentState()
{
switch (State)
{
case CharacterState.Idle:
if(currentAction == "")
Idle();
else
Prune();
break;
case CharacterState.WalkingRight:
WalkingRight();
break;
case CharacterState.WalkingLeft:
WalkingLeft();
break;
case CharacterState.Jumping:
break;
case CharacterState.Falling:
break;
case CharacterState.Hitting:
break;
case CharacterState.Pruning:
Prunning();
break;
}
yield return null;
}//ProcessCurrentState()
..........
void Prunning()
{
if(!anim.isPlaying())
{
Debug.Log("Prunning");
State = CharacterState.Idle;
currentAction = "";
anim.Play("idle");
}
}//Prunning()
public void Prune()
{
Debug.Log("Start to prune");
State = CharacterState.Pruning;
anim.Play("prune_left");
}//Prune()