mecanim animation while bool=true? Problem: animation restarts every frame! C#
Hey there, I'm a quite new to game-coding, but after coding all the gameplay elements of my horror-game I started creating a dirty and simple AI-system using mecanim animations and here comes the problem.
Codesnippets below the explanaition!
Problem: the mecanim animations for the enemy restart every-frame
Setup:
the capsule has my "aisystem"-script to move/attack/idle the position of the enemy and the "lookattarget"-script to get the players direction.
the two sphere-collider "WALKRANGE"(scale(4/4/4)) & "ATTACKRANGE"(scale(2/2/2)) have a simple OnTriggerEnter/exit-script wich correspond with the "aisystem"-script.
unitychan's animations get called through booleans in the update() of the "aisystem"-script
Desired Outcome: the Capsule moves the unity-chan model while she plays corresponding animations (walk forward / attack / idle)
I really appreciate any kind of help! Since my Idea was to call the animations in a while-loop in the update() :D :D :D If you know any fitting tutorials, if possible readable ones, be sure to post them! All necessary pictures and codesnippets are added below!
and sry for my english,
Kim
Hierachysetup
aisystem (move capsule with children & trigger enemymodel's animations)
using UnityEngine;
using System.Collections;
public class aisystem : MonoBehaviour {
public GameObject enemy;
public GameObject enemymodel;
public bool move = false;
public bool attack = false;
public float smooth = 1;
public Animator enemymovement;
// Use this for initialization
void Start () {
enemymovement = enemymodel.GetComponent<Animator>();
}
public void StartMovement()
{
move = true;
}
public void StopMovement()
{
move = false;
}
public void StartAttack()
{
attack = true;
}
public void StopAttack()
{
attack = false;
}
// Update is called once per frame
void Update () {
if(move==true)
{
enemy.transform.Translate(Vector3.forward * smooth * Time.deltaTime);
enemymovement.Play("WALK00_F");
}
if (attack==true)
{ enemymovement.Play("WAIT04"); }
if (move==false)
{
enemymovement.Play("WAIT01");
}
}
}
trigger movement
using UnityEngine;
using System.Collections;
public class aimove : MonoBehaviour {
public GameObject enemy;
// Use this for initialization
void Start () {
}
void OnTriggerEnter(Collider moverange)
{
if (moverange.tag == "Player")
{
enemy.transform.GetComponent<aisystem>().StartMovement();
}
}
void OnTriggerExit(Collider moverangeout)
{
if (moverangeout.tag == "Player")
{
enemy.transform.GetComponent<aisystem>().StopMovement();
}
}
// Update is called once per frame
void Update () {
}
}
trigger attack
using UnityEngine;
using System.Collections;
public class aiattack : MonoBehaviour {
public GameObject enemy;
// Use this for initialization
void Start () {
}
void OnTriggerEnter(Collider attackrange)
{
if (attackrange.tag == "Player")
{
enemy.transform.GetComponent<aisystem>().StartAttack();
enemy.transform.GetComponent<aisystem>().StopMovement();
}
}
void OnTriggerExit(Collider attackrangeout)
{
if (attackrangeout.tag == "Player")
{
enemy.transform.GetComponent<aisystem>().StopAttack();
enemy.transform.GetComponent<aisystem>().StartMovement();
}
}
// Update is called once per frame
void Update () {
}
}
Answer by kayb14 · Jun 20, 2016 at 08:50 AM
It works just fine if I cut out the transitions in the animatior, if not it switches between states and transitions almost each frame
Your answer
![](https://koobas.hobune.stream/wayback/20220612083304im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Humanoid Animations 0 Answers
How do I remove the latency/delay/pause when starting a clip with animator.Play()? 0 Answers
Sync Anima2D Animations with Player Movement 0 Answers
Mecanim Generic No movement 0 Answers
´How to check if 3D player is moving? 0 Answers