Toggle Trigger with Animated Door
Hey guys, im pretty new to Unity and accidently deleted a few of my scripts which now force me to redo the work of the past few hours, so i figured i might aswell do it properly and make a single script that i can reuse for several occasions. So here is the script that i am trying to make:
It's supposed to be an activation lever (either collision, or key trigger) to Open and/or Close a door (Different game object). The lever is supposed to have an animation and sound to itself (when activated go down and when deactivated go back into original position), aswell as the door.
So the Scenario i am trying to achive is basically this: 3rd Person Character walks up to Lever, actiavtes it (either collision or key), sound plays, animation for lever plays, Animation and sound for respective Door plays. Door then waits for lever to be activated again which inverts the process ending up with the starting situation, or alternativly, after first useage, lever gets deactivated, door remains where the first animation leaves it.
I have several issues: 1. Having animatiors that wait for actiavtion to play the next animation (so far my animation circles just permanently loop, and i dont know how to adress the triggers or bools of the animator or another object in order to set it up correctly) 2. Adressing 2 Animators in 2 different objects using only 1 code 3. The ability to set up if i want the lever to be a 1time-use-lever or a multiple-time-toggle-lever
So far i solved the issue with the one time door, by deactivating and activating the script in the respective door, but that doesnt work for a Toggle Door. Here is my very messy attempt on my first Script, which also relies on a Script in the target door to be activated (so not a good solution). Help would be very very much apreaciated!
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class CollisionSound : MonoBehaviour {
public GameObject otherObject;
public GameObject thisObject;
private bool currentState = true;
public bool wantedState;
public bool OneTime;
public float speed;
public AudioClip SoundtoPlay;
AudioSource audio;
public float Volume;
[SerializeField] private GameObject uiElement;
private bool moveable;
// Use this for initialization
void Start () {
audio = GetComponent<AudioSource>();
}
// Update is called once per frame
void Update()
{
if (moveable == true)
{
transform.Rotate(new Vector3(Time.deltaTime * speed* 0, 0, 45));
moveable = false;
thisObject.GetComponent<CollisionSound>().enabled = wantedState;
}
}
private void OnTriggerStay(Collider other)
{
if (other.CompareTag("Player") && currentState)
{
uiElement.SetActive(true);
if (Input.GetKeyDown("joystick button 1") || Input.GetKeyDown(KeyCode.E))
{
audio.PlayOneShot(SoundtoPlay, Volume);
if (OneTime == true)
{
otherObject.GetComponent<SlideDoor>().enabled = wantedState;
moveable = true;
uiElement.SetActive(false);
currentState = false;
}
}
}
}
private void OnTriggerExit(Collider other)
{
if (other.CompareTag("Player"))
{
uiElement.SetActive(false);
}
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612183756im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How do I not trigger animation during play state of the triggered animation. 1 Answer
Why is the last frame of the animation not triggering? 0 Answers
Editing other object's animator parameter... NullReferenceException? (VR) 0 Answers
Death animation for enemy 0 Answers
How to add programatically animationClips to one animation from Resources folder? 0 Answers