Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Owensven · May 03, 2017 at 11:41 AM · animationscripting problemtroubleshooting

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.

Comment
Add comment · Show 3
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image SohailBukhari · May 03, 2017 at 01:54 PM 2
Share

post code what you tried.No one here will write script for you , ask for help where you stuck off.

avatar image dhar174 · May 03, 2017 at 02:33 PM 0
Share

@Owensven

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

avatar image eaglespy_21 · May 04, 2017 at 02:09 AM 1
Share

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;
      }
  }
 

3 Replies

· Add your reply
  • Sort: 
avatar image
0

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;
     }
 }


Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image ronahattingh · Jul 03, 2017 at 01:55 PM 0
Share

Thank you so much, this bit of code just helped me a lot.

avatar image
0

Answer by sandeepsmartest · May 04, 2017 at 07:31 AM

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


link text


simpleanimsetup.zip (23.8 kB)
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

8 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to check in code when an animation is over? 1 Answer

Hi.How to change Spine Animation of a game object through a script that is Attached to another game object? 0 Answers

run animation when should idle 1 Answer

Inputs and animations interfering with each other 1 Answer

How to make door open and close with Input.GetButtonDown? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges