- Home /
Other
Why does my OnTriggerEnter plays my animation?
Hi!! I am new to coding for my project and I encounter an issue. My scene is that players are able to use the keyboard or teleporters on the ground to move. While they enter different rooms, the room names are display on screen and disappears. This animation is attached onto a collider. But I have an animation in the room too. So when i enter the room, the name displays but when i click on the teleporter(it teleports the player) but also plays an animation that ISNT ATTACH TO IT.
When i go on play mode and click on the object the animation is attach to, it works fine. But when I click on the teleporters, THE ANIMATION PLAYS TOO!
$$anonymous$$y animation script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class $$anonymous$$olecularAnimation : $$anonymous$$onoBehaviour
{
public Animator anim;
bool open = false;
// Start is called before the first frame update
void Start()
{
anim = gameObject.GetComponent<Animator>();
}
// Update is called once per frame
void Update()
{
if (Input.Get$$anonymous$$ouseButtonDown(0))
{
open = !open;
if (open)
{
anim.SetBool("opened", true);
}
else
{
anim.SetBool("opened", false);
}
}
}
Area name trigger code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class $$anonymous$$olecular_Label : $$anonymous$$onoBehaviour
{
public Text displaytext;
public Animator textanimation;
private void OnTriggerEnter(Collider Player)
{
displaytext.text = "$$anonymous$$OLECULAR DIAGNOSTIC TECHNOLOGY LAB";
textanimation.Play("AnimationText", -1, 0f);
}
private void OnTriggerExit(Collider Player)
{
displaytext.text = "";
}
}
Answer by MurphyMurph_21 · Dec 30, 2020 at 04:16 PM
So I think your problem is in the MolecularAnimation script. in your update method you are playing an animation every time you click the mouse button. So even if one animation is playing your play "opened" animation everytime you click the mouse even if its not a a teleporter
void Update()
{
if (Input.GetMouseButtonDown(0))
{
open = !open;
if (open)
{
anim.SetBool("opened", true);
}
else
{
anim.SetBool("opened", false);
}
}