- Home /
Question by
DanProd · Mar 13, 2019 at 02:33 PM ·
animationscripting problemaiartificial intelligencebehaviour
Conflicting animations (attack and idle)
Basically i just want an ai that has animations when attacking, waiting and walking/running...Any clue on how to achieve this? below is a code i made that acheived both waiting and running/walking but is flickering or somewhat broken when i add the attack animation
NavMeshAgent agent;
public Transform target;
public float extraRotationSpeed;
public float AttackmaxDistance = 3;
public float damage;
public float hitLast = 0;
public float hitDelay = 5;
public bool wander = true;
public float wanderRadius;
public float wanderTimer;
public float awareness = 20f;
public bool Animations;
private Transform targetrandom;
private float timer;
public Animator anim;
public string walkAnimation;
public string idleAnimation;
public string attackAnimation;
Vector3 previousPos;
void Start ()
{
agent = GetComponent<UnityEngine.AI.NavMeshAgent> ();
wander = true;
target = GameObject.Find("Player").transform;
}
void Update()
{
if (Animations == true)
{
if (previousPos != transform.position) anim.Play(walkAnimation);
else anim.Play(idleAnimation);
previousPos = transform.position;
}
CheckPlayer();
attackPlayer();
anim.Play(attackAnimation);
if (wander == false)
{
agent.SetDestination(target.position);
{
extraRotation();
}
}
if (wander == true)
{
timer += Time.deltaTime;
if (timer >= wanderTimer)
{
Vector3 newPos = RandomNavSphere(transform.position, wanderRadius, -1);
agent.SetDestination(newPos);
extraRotation();
timer = 0;
}
}
}
void extraRotation()
{
Vector3 lookrotation = agent.steeringTarget - transform.position;
transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(lookrotation), extraRotationSpeed * Time.deltaTime);
}
void attackPlayer()
{
float playerDistanceFromCharacterToAttack = Vector3.Distance(target.transform.position, transform.position);
if (playerDistanceFromCharacterToAttack < AttackmaxDistance)
{
anim.Play(attackAnimation);
if (Time.time - hitLast < hitDelay)
return;
target.gameObject.GetComponent<PlayerHealth>().PlayerTakeDamage(damage);
hitLast = Time.time;
}
else
{
//extra stuff when out of reach
}
}
public static Vector3 RandomNavSphere(Vector3 origin, float dist, int layermask)
{
Vector3 randDirection = Random.insideUnitSphere * dist;
randDirection += origin;
NavMeshHit navHit;
NavMesh.SamplePosition(randDirection, out navHit, dist, layermask);
return navHit.position;
}
void CheckPlayer()
{
float playerDistanceFromCharacter = Vector3.Distance(target.transform.position, transform.position);
Debug.DrawLine(target.transform.position, transform.position);
if (playerDistanceFromCharacter < awareness)
{
wander = false;
}
else
{
wander = true;
}
}
Comment
cleaned your formatting, please use the code format tool when pasting large blocks.
Best Answer
Answer by Vollmondum · Mar 13, 2019 at 03:32 PM
Since you're working with Animator, not Animation, you need to setup your Mecanim correctly (Animator window). You're playing animations from Animator, and all transitionas are made from withing Animator, not coding. In lines you tell it to play some animation, use anim.SetTrigger("myTrigger") or anim.SetBool("myBool", true/false) unstead.