- Home /
Have some minor problems. Cant tell if its a script issue or a animator issue.
I have a enemy ai that plays these animations through the animator. The code normally looked like this.
public class Chase : MonoBehaviour {
public Transform player;
static Animator anim;
// Use this for initialization
void Start ()
{
anim = GetComponent<Animator>();
}
// Update is called once per frame
void Update ()
{
Vector3 direction = player.position - this.transform.position;
float angle = Vector3.Angle(direction,this.transform.forward);
if(Vector3.Distance(player.position, this.transform.position) < 15 && angle < 90)
{
direction.y = 0;
this.transform.rotation = Quaternion.Slerp(this.transform.rotation,
Quaternion.LookRotation(direction), 0.1f);
anim.SetBool("isIdle",false);
if(direction.magnitude > 3)
{
this.transform.Translate(0,0,0.05f);
anim.SetBool("isWalking",true);
anim.SetBool("isAttacking",false);
}
else
{
anim.SetBool("isAttacking",true);
anim.SetBool("isWalking",false);
}
}
else
{
anim.SetBool("isIdle", true);
anim.SetBool("isWalking", false);
anim.SetBool("isAttacking", false);
}
}
}
I wanted to add blocking into my game so i thought of this. What if i added a collider script so when the enemies sword made contact with the shield it will instantly play the animation. That's were i came up with this other script that would do this from another object. Now ive made this code work by having the enemy have a collider on his head that when i hit the enemy would play the "isDead" animation.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Blocked : MonoBehaviour
{
public GameObject _gameObject;
void Update ()
{
}
void OnTriggerEnter (Collider col)
{
if (col.gameObject.tag == "OhNo")
{
_gameObject.GetComponent<Animator>().SetBool ("isBlocked", true);
}
}
}
Currently with this the enemy hits don't do anything. I have to shake the shield in front of him to get it to eventually activate. Once it is activated he keeps playing it over and over again. When i walk away he keeps jumping from "isWalking" to "isBlocked". Jolly cooporation with Eliminating the problem that causes this terribly malfunction would be greatly appreciated
Answer by Dylanpolis · Aug 04, 2017 at 11:59 PM
Simple Fix. Just had to add a OnTriggerExit. Thanks for the help!
void OnTriggerExit (Collider col)
{
_gameObject.GetComponent<Animator> ().SetBool ("isBlocked", false);
}