In my scene there is a character (a troll) and a First Person Character, like in the picture.
The troll has to states: “Walk” and “Idle_02”. A bool variable “walking” insures to pass from Idle_02 to Walk (walking = true) and Walk to Idle_2 (walking = false).
The code is attached to Trol character and is as follows:
#pragma strict
public var walking: boolean = false;
public var anim: Animator;
public var currBaseState: AnimatorStateInfo;
public var walkState: int = Animator.StringToHash("Base.Walk");
public var idleState:int = Animator.StringToHash("Base.Idle_02");
private var playerTransform: Transform;
private var hit: RaycastHit;
function Start () {
anim = GetComponent(Animator);
anim.Play("Walk");
playerTransform = GameObject.FindGameObjectWithTag("Player").transform;
}
function Update () {
currBaseState = anim.GetCurrentAnimatorStateInfo(0);
var distance:float = Vector3.Distance(transform.position, playerTransform.position);
Debug.Log("Current base state: " + currBaseState);
switch(currBaseState.nameHash){
case idleState: Debug.Log("Idle state !!!!!!!!!!!!!");
if ((Physics.Raycast(Vector3(transform.position.x,
transform.position.y+.5,
transform.position.z),
transform.forward, hit,40) &&
hit.collider.gameObject.tag == "Player") || distance <4.0f){
anim.SetBool("walking",true);
}
break;
case walkState: Debug.Log("Walk state !!!!!!!!!!!!!");
transform.LookAt(playerTransform);
break;
default: Debug.Log("Default state !!!!!!!!!!!!!");
break;
}
transform.rotation.x = 0.0;
transform.position.y = -0.5;
transform.rotation.z = 0.0;
}
If I get closer to troll, it should walk to the player. But when I play the troll stays in Idle_02 state.
currBaseState is always in default state. The message in console is “Default state !”. It does not change the state.