How to play an animation with a trigger?

So I’m a complete newbie to unity, and this is my first game that i’m working on. Its a maze game, and i wanted to add a simple puzzle to it, where you could push a ball into an area and it would play an animation to lower a door, and it would play another animation to raise the door when the object left the collider . I made the ball, the collider (and set it to trigger), and now i just need a script to run it. I’ve tried some of the OnTrigger… scripts, and tried to modify them but they never seem to work. I tried using someone else’s script that i found from a youtube tutorial and it also did not work. So what i’m asking here is if anyone could give me a script that would work for what i’m doing, or if they could direct me to a tutorial or post that would be helpful. Thanks for your time.

This is a simple script to lower a door (3d plane) when triggered by a player(rigidbody). I believe by animation this is what you meant.
However, I agree with @SohailBukhari. If you tried something why not post it here so that someone can help you fix it. It will only give you more confidence. Also, there is lots of resource material available online, I know being a beginner it is hard to digest a lot of it. But you must. That’s the only way.

public class LoweringDoor : MonoBehaviour {
    public Vector3 setPointA, setPointB = Vector3.zero;
    public bool up;
    public float speed=5, E = 1;
	// Use this for initialization
	void Start () {
		
	}
	
	// Update is called once per frame
	void FixedUpdate () {
        if (up)
        {
            if(transform.position.y - setPointB.y >= E)
            {
                print("Inside while");
                transform.Translate(Vector3.down * speed * Time.deltaTime, Space.World);
            }
            else
            {
                up = false;
            }
        }
	}
    void OnTriggerEnter(Collider other)
    {
        print("Triggered");
        if (!up)
            up = true;
    }
}

I don’t know if I got this straight but you are asking for a script that would allow you to play an animation on trigger. This is pretty simple actually. Firstly you need to set a parameter on your animator controller, a bool parameter. If the bool is true the lower animation will play and if not the raise animation will play. You also will need an idle animation. If you don’t know anything about this here it is a tutorial Controlling Animation - Unity Learn.
The script is pretty simple actually.

private void OnTriggerStay(Collider other)
{
if (other.gameObject.tag == "Player")
anim.SetBool("boolName", true);
}

private void OnTriggerExit(Collider other)
{
anim.SetBool("boolName", false);
}

where “anim” is a reference to the Animator (declared at the begging) and “boolName” is the parameter created before on the Animator controller.

Hi,
Please find the attachment which consists of unity package .

  1. Just import it in your test project or empty project

  2. select the scene “SimpleAnimSetup” and hit play button

  3. Move the player forward towards door and backward by simple pressing up and down arrows.Observer the Door up and down animation.

  4. Since you are trying with animation i made a basic animation clip but the same up down animation can be achieved by translating/changing door pos up and down

This is very simple and basic animation demo based on 2 animations “Door up” and “Door down” with help of OnTriggerEnter and OnTriggerExit.


Note: there are many ways to achieve this .This is just a demo for very basic understanding.


Hope this may help you.


Nsks


[93385-simpleanimsetup.zip|93385]