• 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 /
  • Help Room /
avatar image
0
Question by researchmore · Nov 05, 2020 at 08:11 PM · toggleboolkey

toggle animator bool when in a collider

Hello, Im fairly new to c# and can't find a way to apply a toggle on a bool. Thank you for your help!

I am trying to create an information zone. When the player enter a collider, a text appears saying "press use key" to read. Then an animation pulling the information text in and out the screen is controlled by a bool named "slide".

I would like to set the boolean controlling the animation to true or false with a single key while in the collider, so the player can either move away from the collider or press "use" again to exit the text.

With the following code I only press "use" once and move away from the collider to make disapear. I am sure there is a simple solution to this but smh can't apply it. Thanks again!!

   using System.Collections;
     using System.Collections.Generic;
     using UnityEngine;
     
     public class OnTriggerLoadconversation : MonoBehaviour
     {
       [SerializeField] private Animator myAnimationController;
       public GameObject enterText;
     
       void Start()
       {
         myAnimationController.SetBool("slide", false);
         enterText.SetActive(false);      
       }
     
       // Update is called once per frame
       void OnTriggerEnter(Collider plyr)
       {
           if (plyr.gameObject.tag == "Player")
           {
             enterText.SetActive(true);
     
               if (Input.GetButtonDown("Use"))
               {
                 myAnimationController.SetBool("slide", true);
                 enterText.SetActive(false);
     
               }
           }        
       }

       void OnTriggerStay(Collider plyr)
       {
           if (plyr.gameObject.tag == "Player")
           {
     
               if (Input.GetButtonDown("Use"))
               {
                 myAnimationController.SetBool("slide", true);
                 enterText.SetActive(false);
     
                }
             }
       }

       void OnTriggerExit(Collider plyr)
       {
           if (plyr.gameObject.tag == "Player")
           {
             myAnimationController.SetBool("slide", false);
             enterText.SetActive(false);
     
          }
       }
     }
     
Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by researchmore · Nov 08, 2020 at 10:39 AM

So I made it work with the following solution. It still seem a bit buggy. (for some reason it doesn't seem to work every time) I am open if any body has something more elegant :)

So this is applied to my collider object. The ui object (press x to display info) is called "enterText". The bool that animate the actual information text in and out screen is called "slide", and Is toggled false or true to make it move.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class OnTriggerLoadconversation : MonoBehaviour
 {
   [SerializeField] private Animator myAnimationController;
   public GameObject enterText;
 
   void Start()
   {
     enterText.SetActive(false);
     myAnimationController.SetBool("slide", false);
     }

    // Update is called once per frame
     void OnTriggerEnter(Collider plyr)
   {
       if (plyr.gameObject.tag == "Player")
       {
         enterText.SetActive(true);       
       }
   }

 void OnTriggerStay(Collider plyr)
   {
       if (plyr.gameObject.tag == "Player")
       {
             if (Input.GetButtonDown("Use"))
           {
    myAnimationController.SetBool("slide", !myAnimationController.GetBool("slide"));
    enterText.SetActive(!enterText.activeInHierarchy);
           }
         }
   }

    void OnTriggerExit(Collider plyr)
     {
         if (plyr.gameObject.tag == "Player")
         {
        enterText.SetActive(false);
        myAnimationController.SetBool("slide", false);
         }
     }
 }


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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

210 People are following this question.

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

Related Questions

Toggle Group to make a GameObject Active in Scene onClick 0 Answers

How to enable/disable an Icon depending on the bool state of a toggle. 0 Answers

Help with Arrays - Check if only 1 is toggled 2 Answers

Dropdown sets all values to 1 above what they should be 1 Answer

my sprint script is toggling? help please! 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges