4
\$\begingroup\$

I've been stuck on this for hours.

I have a character whose animations are based in mecanim to transition based on the bool variable ShivaEnter. One invisible cube is set to change the bool value from false to true and vice-versa OnTriggerEnter.

The code is as follows:

using UnityEngine;
using System.Collections;

public class PontyAnimations : MonoBehaviour {

  //[SerializeField]
  private Animator animator;
  //private GameObject ponty;
  //private GameObject ponty;

  void Awake () {
    animator = GetComponent <Animator>();
  }

  void OnTriggerEnter (Collider other) {
    if (other.gameObject.tag == "Player") {
      animator.SetBool ("ShivaEnter", true);
    }
  }

  void OnTriggerExit (Collider other) {
    if (other.gameObject.tag == "Player") {
      animator.SetBool ("ShivaEnter", false);
    }
  }
}
\$\endgroup\$
4
  • \$\begingroup\$ What does exactly the code do? Probably does not trigger the animation, but better to say that explicitly. Is the if statement ever triggered? \$\endgroup\$
    – wondra
    Nov 15 '15 at 20:56
  • \$\begingroup\$ The if statement is not triggered. There is no change in animation occurring. \$\endgroup\$
    – ledzee
    Nov 15 '15 at 21:01
  • \$\begingroup\$ Have you enabled isTrigger at your collider? \$\endgroup\$
    – ElDuderino
    Nov 15 '15 at 21:01
  • \$\begingroup\$ If you never entered the if (other.gameObject.tag == "Player") { ...} (did we meant the same thing?), then the problem lies in collision detection rather than in animation. \$\endgroup\$
    – wondra
    Nov 15 '15 at 21:02
1
\$\begingroup\$

I changed the code as following and edited the transitions to be more sudden! It worked.

using UnityEngine;
using System.Collections;

public class boxponty : MonoBehaviour {

  public bool ShivaEnter;
  private GameObject player;
  private Animator anim;
  private AudioClip pontysound;

  // Use this for initialization
  void Start () {
  }

  // Update is called once per frame
  void Update () {
    anim = GetComponent<Animator>();
  }

  void OnTriggerEnter (Collider other){
    anim.SetBool ("ShivaEnter", true);
    GetComponent<AudioSource>().Play();
  }

  void OnTriggerExit (Collider other){
    anim.SetBool ("ShivaEnter", false);
  }
}
\$\endgroup\$
1
  • \$\begingroup\$ You don't need to change Start to Update. It only gets slower this way. \$\endgroup\$
    – ElDuderino
    Nov 15 '15 at 23:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.