Need help with bone animation and trigger code.
I'm currently trying to write a code where a certain animation for a model plays when the character is within a certain distance away from the model. I'm having some difficulty however, because I've never used Bone Animations, which the model uses, and have been told they work differently than other animations. I've tried multiple ways of trying to make it work, but to no avail. Can anyone point me towards the right direction and help me figure out what/if I'm doing wrong? I'm fairly new at using animations, so I'm very lost at the moment.
Link for model: https://www.assetstore.unity3d.com/en/#!/content/3547
My Code (was previously used for a door):
bool doorisOpen =false;
float doorTimer = 0.0f;
public float doorOpenTime = 3.0f;
public AudioClip doorOpenSound;
public AudioClip doorShutSound;
// Use this for initialization
void Start () {
doorTimer = 0.0f;
}
// Update is called once per frame
void Update () {
if (doorisOpen) {
doorTimer += Time.deltaTime;
}
if(doorTimer > doorOpenTime){
Door (doorShutSound, false, "whalesound");
doorTimer = 0.0f;
}
}
void OnControllerColliderHit(ControllerColliderHit hit) {
if (hit.gameObject.tag == "whale"&&doorisOpen==false){
Door(doorOpenSound, true, "whalesound");
}
}
void OpenDoor (GameObject humpback_whale_model23 ){
doorisOpen = true;
AudioSource doorAudio = GetComponent < AudioSource>();
doorAudio.clip = doorOpenSound;
doorAudio.Play ();
humpback_whale_model23.transform.parent.GetComponent <Animation>().Play ("dive");
}
void CloseDoor (GameObject humpback_whale_model23) {
doorisOpen = false;
AudioSource doorAudio = GetComponent <AudioSource>();
doorAudio.clip = doorOpenSound;
doorAudio.Play();
humpback_whale_model23.transform.parent.GetComponent <Animation>().Play ("dive");
}
void DoorCheck(){
if (!doorisOpen){
Door(doorOpenSound, true, "whalesound");
}}
void Door (AudioClip aClip, bool openCheck, string animName){
AudioSource doorAudio = GetComponent <AudioSource>();
doorAudio.clip = aClip;
doorAudio.Play ();
doorisOpen = openCheck;
transform.parent.GetComponent <Animation> ().Play (animName);
}
}
My Trigger zone code.:
void Start () {
}
// Update is called once per frame
void Update () {
}
void OnTriggerEnter(Collider col){
if (col.gameObject.tag == "Player"){
transform.FindChild("humpback_whale_model23").SendMessage ("DoorCheck");
}
}}
Your answer
Follow this Question
Related Questions
Play Animation on key pressed 0 Answers
How do I check if the x position of my object is 0? 1 Answer
Playing animation without hold the button 1 Answer
Really NEED help about Animation 1 Answer