Question by
AsAnAsterisk · Jun 26, 2017 at 06:15 AM ·
animationscripting problemmovementaienemy ai
How To Stop Enemy Movement During Its Attack Animation
I have an enemy and when it's doing its attack animation I would like it to stop moving and rotating until the animation finishes. I'm sure it involves setting the force to 0 and whatnot but I'm unsure how to phrase the coding in a way that achieves this effect
public class Chase : MonoBehaviour {
public Transform player;
public CapsuleCollider cc;
private Animator anim;
private Rigidbody myRigidbody;
private Vector3 moveDirection;
public Collider attack;
public Slider healthbar;
void OnTriggerEnter(Collider attack)
{
if (attack.gameObject.tag == "PlayerAttack") {
healthbar.value -= 20;
anim.SetBool("IsInjured", true);
}
}
// Use this for initialization
void Start()
{
anim = GetComponent<Animator>();
myRigidbody = GetComponent<Rigidbody>();
cc.enabled=false;
}
// Update is called once per frame
void Update()
{
moveDirection = Vector3.zero;
if (Vector3.Distance(player.position, this.transform.position) < 4)
{
Vector3 direction = player.position - this.transform.position;
direction.y = 0;
this.transform.rotation = Quaternion.Slerp(this.transform.rotation,
Quaternion.LookRotation(direction), 0.3f);
anim.SetBool("IsIdle", false);
if (direction.magnitude > .8)
{
moveDirection = direction.normalized;
anim.SetBool("IsMoving", true);
anim.SetBool("IsBiting", false);
anim.SetBool("IsInjured", false);
}
else
{
anim.SetBool("IsBiting", true);
anim.SetBool("IsMoving", false);
}
}
else
{
anim.SetBool("IsIdle", true);
anim.SetBool("IsMoving", false);
anim.SetBool("IsBiting", false);
}
if (healthbar.value <= 0){
anim.SetBool("IsVulnerable", true);
}
else
{
anim.SetBool("IsVulnerable", false);
}
}
public void ActiveCollider(int active){
if (active == 0)
cc.enabled = false;
if (active == 1)
cc.enabled = true;
}
public void VulnerableRegain(int active){
if (active == 1)
healthbar.value += 60;
}
void FixedUpdate()
{
myRigidbody.AddForce(moveDirection * 70f);
}
}
Comment
Best Answer
Answer by AsAnAsterisk · Jun 26, 2017 at 11:30 PM
I solved this myself, actually, I just put this at the end of my code
public void StopMovement(int active) {
if (active == 1)
transform.GetComponent<Rigidbody>().constraints = RigidbodyConstraints.FreezePositionY | RigidbodyConstraints.FreezeRotationX | RigidbodyConstraints.FreezeRotationY | RigidbodyConstraints.FreezeRotationZ;
if (active == 0)
transform.GetComponent<Rigidbody>().constraints = RigidbodyConstraints.FreezeAll;
}
And then put animation events with the function "StopMovement" on all the animations, i tried only doing it with the attack animations but it ended up completely freezing the enemy, so I needed to put StopMovement int=1 animation event at the start of the rest of the animations so it would still move normally.