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);
}
}
}
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);
}
}
}
Your answer
Follow this Question
Related Questions
Toggle a bool on only one object at a time by tapping an object. 0 Answers
How to enable/disable an Icon depending on the bool state of a toggle. 0 Answers
Toggle Group to make a GameObject Active in Scene onClick 0 Answers
How do you keep a toggle highlighted/ load up a scene with the toggle in its highlighted state 1 Answer
How can I set a toggle to reverse the direction of a rotation? 0 Answers