Meeting Animation parameters with an AI sprite.
I am working on a top down 2d game sorta like a mix between pokemon and river city ransom, where the player controls an animated sprite with the arrow keys. I am reusing the animation controller for an ai sprite, animated in a smillar fashion. I will include a copy of my script. What I am attempting to do is, based on the direction my ai is traveling, fulfill the parameters and have him animate correctly. with my code, i cannot seem to find a way to pass a 1 or -1 to my animation parameter, which is achieved in my playermovement script with getaxis.raw I'm not very experienced with scripting and i've searched high and low for answers with no avail, so some help would be much obliged. using UnityEngine; using System.Collections; public class Chase_AI : MonoBehaviour { Animator anim; public Transform target;//set target from inspector instead of looking in Update Vector2 playerPos, enemyPos, previousdir; void Start() { anim = GetComponent (); } void Update() { playerPos = new Vector2(target.localPosition.x, target.localPosition.y);//player position enemyPos = new Vector2(this.transform.localPosition.x, this.transform.localPosition.y);//enemy position if (Vector3.Distance(transform.transform.position, target.transform.position) > 1.6)//move towards if not close by { transform.position= Vector2.MoveTowards(enemyPos, playerPos, 2 Time.deltaTime); }else if (Vector3.Distance(transform.transform.position, target.transform.position) < 1.55)//move away if too close { transform.position = Vector2.MoveTowards(enemyPos, playerPos, -3 Time.deltaTime); } previousdir = enemyPos;
if (previousdir != Vector2.zero) {
anim.SetBool ("iswalking", true);
anim.SetFloat ("input_x", previousdir.normalized.x);
anim.SetFloat ("input_y", previousdir.normalized.y);
} else {
anim.SetBool ("iswalking", false);
}
}
}