- Home /
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.
post code what you tried.No one here will write script for you , ask for help where you stuck off.
Have you used OnTriggerEnter and connect it to the collider?
Something like this:
private void OnTriggerEnter(Collider col) { if(col.gameObject.tag == "Ball") {
and insert what you want to happen after the if. I haven't used it with animations yet but it seems to work well for other uses, such as level exits, and I imagine it would work just fine with the GetComponent function.
For more help with animations, check out: https://docs.unity3d.com/ScriptReference/Animation.html
Here is some info on using OnTriggerEnter (which you can combine with any other function, not just what is used in the example): https://docs.unity3d.com/ScriptReference/Collider.OnTriggerEnter.html
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 : $$anonymous$$onoBehaviour {
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;
}
}
Answer by eaglespy_21 · May 05, 2017 at 08:46 AM
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;
}
}
Answer by $$anonymous$$ · May 03, 2017 at 05:38 PM
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 https://unity3d.com/learn/tutorials/topics/animation/animator-controller. 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.
Thank you so much, this bit of code just helped me a lot.
Answer by sandeepsmartest · May 04, 2017 at 07:31 AM
Hi, Please find the attachment which consists of unity package .
Just import it in your test project or empty project
select the scene "SimpleAnimSetup" and hit play button
Move the player forward towards door and backward by simple pressing up and down arrows.Observer the Door up and down animation.
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