- Home /
Question by
murraythemoose · Jan 24, 2018 at 10:56 PM ·
animationscripting problemscriptingbasicsanimator controller
Issue trying to animate multiple models with same controller and script
Hello! I have one character model in my scene and I have scripted it to execute animations from its controller under certain conditions relative to the players position. However, if I duplicate the character model and add the same script and controller as the original model, the animations do not activate properly for the new model. I think it might be some sort of area conflict or something, but I'm pretty new to unity so I'm not sure. Here is my script that I am trying to use for the models:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class chase : MonoBehaviour {
public Transform player;
static Animator anim;
public Slider healthbar;
void Start () {
anim = GetComponent<Animator>();
}
void Update () {
if (healthbar.value <= 0) return;
Vector3 direction = player.position - this.transform.position;
float angle = Vector3.Angle(direction, this.transform.forward);
if (Vector3.Distance(player.position, this.transform.position) < 10 && angle < 120)
{
direction.y = 0;
this.transform.rotation = Quaternion.Slerp(this.transform.rotation, Quaternion.LookRotation(direction), 0.1f);
anim.SetBool("IsIdle", false);
if (direction.magnitude > 5)
{
this.transform.Translate(0,0, 0.043f);
anim.SetBool("IsRunning", true);
anim.SetBool("IsAttacking", false);
}
else
{
anim.SetBool("IsAttacking", true);
anim.SetBool("IsRunning", false);
}
} else
{
anim.SetBool("IsIdle", true);
anim.SetBool("IsRunning", false);
anim.SetBool("IsAttacking", false);
}
}
}
Comment