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()
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 () {
}
}