How to make my Power Button stop my Conveyor Belt
I have two scripts, one on the Button that's a trigger volume, and the other is the conveyor belt itself. The conveyor belt script is simple it reads,
using System.Collections; using System.Collections.Generic; using UnityEngine; public class Belt : MonoBehaviour { public GameObject belt; public Transform endpoint; public float speed; public bool beltOn; void OnTriggerStay(Collider other) { if(beltOn) { other.transform.position = Vector3.MoveTowards(other.transform.position, endpoint.position, speed * Time.deltaTime); } } }
Then, the next script is the script attached to the trigger volume near the power button.
using System.Collections; using System.Collections.Generic; using UnityEngine; public class Trigger : MonoBehaviour { //Boolean used to check if the player is in the trigger volume. private bool isHere = false; private GameObject conveyorbelt; public GameObject RedButton; public Light ButtonLight; private Renderer Button_Rend; public Material Red; public Material Green; void Start() { //Assign our private variable equal to the Button's Renderer component. Button_Rend = RedButton.GetComponent(); } //Start of a function to swap light to green. void GreenButton() { //Set intensity. ButtonLight.intensity = 4; //Set color. ButtonLight.color = Color.green; } //Start of a function to swap light to red. void ButtonRed() { //Set button material. Button_Rend.material = Red; //set intensity ButtonLight.intensity = 4; //set the color. ButtonLight.color = Color.red; } void OnTriggerEnter(Collider entity) { if(entity.tag == "Player") { //If a colliding object tagged as Player enters the Trigger volume, change the value of isHere to true. isHere = true; } } //Function called when an object exits the trigger volume. void OnTriggerExit(Collider entity) { //If the object that has exited the trigger volume has the Player tag. if(entity.tag == "Player") { //If a colliding object tagged as Player exits the Trigger, change the value of isHere back to false. isHere = false; } } }
Now that's my code I have thus far. I want a code that can be scripted inside the Trigger Volume script named "Trigger." I want the code to have a requirement of the player being inside the trigger volume, they press the key E, and if the button is red, then the conveyor will turn on. If all those requirements are met and the button is green then I want it to stop the conveyor belt. I can get the button color to change by key presses, but can't get the belt to stop. Sorry if my message isn't the clearest message to read, but I'm just having trouble and didn't know where else to ask for help.
Your answer
Follow this Question
Related Questions
Selecting animation to play based on boolean 1 Answer
Play fbx animation on trigger and keypress 0 Answers
Trigger when holding key 1 Answer
Boolean variable won't trigger in other script 1 Answer
Timer onTriggerEnter doesn't launch 0 Answers