- Home /
Trigger Camera Component (activate and deactivate)
Hi, I have a component in the main camera of the player that has the datamosh effect by Keijiro (https://github.com/keijiro/KinoDatamosh). What I'd like to have is that this component activates only when the player enters in specific areas (with a trigger I suppose) and then deactivates when exits. Is it possible? Thanks!
Answer by dan_wipf · Nov 15, 2018 at 07:49 AM
yes it is, if those zones have a collider setup as trigger.
then you can call in a script OnTriggerEnter() yourcomponent.enable = true; and on exit false.
you need to compare tag in this method. like if(other.tag == “Player”)
EDIT nr 2:
reedit of Code, explanation, and GitHub uploaded Example
using System.Collections;
using UnityEngine;
public class TriggerZoneMOSH : MonoBehaviour {
public float Speed = 1;
[Range(0,1)] public float MaxEntropy;
Kino.Datamosh k_data;
float t = 0;
void Start(){
k_data = Camera.main.GetComponent<Kino.Datamosh>();
k_data.entropy = 0;
}
void OnTriggerEnter(Collider col){
if(col.tag == "TriggerZone"){
StopCoroutine("FadeOut");
t = 0;
k_data.Glitch();
StartCoroutine("FadeIn");
}
}
void OnTriggerExit(Collider col){
if(col.tag == "TriggerZone"){
StopCoroutine("FadeIn");
t = 0;
StartCoroutine("FadeOut");
}
}
IEnumerator FadeIn(){
while (true){
t += Time.deltaTime;
k_data.entropy = Mathf.SmoothStep(0,MaxEntropy,t / Speed);
yield return new WaitForSeconds(0);
if(k_data.entropy == MaxEntropy){
yield break;
}
}
}
IEnumerator FadeOut(){
while (true){
t += Time.deltaTime;
k_data.entropy = Mathf.SmoothStep(MaxEntropy,0,t / Speed);
yield return new WaitForSeconds(0);
if(k_data.entropy == 0){
k_data.Reset();
yield break;
}
}
}
}
Attach this script to the Player. Be sure Datamosh is attached to the Main Camera. Now you create Triggerzones with a Collider (be sure to tick Is Trigger to enabled inside to Collider Property). You have to Tag the TriggerZone now to work with Script. Add a new Tag right under The Name of the GameObject (Dropdown Menu). There's the Option Add Tag.. hit that and give THIS NAME:
TriggerZone
If you choose Something else you have to edit the Script where it says if(col.tag == "TriggerZone") replace the "TriggerZone" with your name which you added above.
now you should be good to go. but you can have a look here at Github. I have uploaded a Demo Scene. with 3 TriggerZones and a simple Player Cube.
THAN$$anonymous$$S @dan_wipf ! but just to make things trickier... Is it possible to have the effect increase/decrease with a fade in/out when player enters/exits? Something like audio reverb zone.
yes, if the component has something like a dry/wet property
can you give me a photo of the inspector?(properties)
WOW! Thanks my dude gonna try it in a few hours and get back here, thanks!
@dan_wipf sorry can't understand where to attach the fragment of the script...I thought was easier
I'm attaching this: using System.Collections; using System.Collections.Generic; using UnityEngine;
public class trigger : $$anonymous$$onoBehaviour {
public float Speed = 1;
void OnTriggerEnter(Collider col){
if(col.tag == "Player"){
StartCoroutine("FadeIn");
}
}
void OnTriggerExit(Collider col){
if(col.tag == "Player"){
StartCoroutine("FadeOut");
}
}
IEnumerator FadeIn(){
while(true){
Camera.main.GetComponent<$$anonymous$$ino.Datamosh>().enabled = true;
GetComponent<$$anonymous$$ino.Datamosh>().entropy = $$anonymous$$athf.SmoothStep(0,1,Time.deltaTime * Speed);
if(GetComponent<$$anonymous$$ino.Datamosh>().entropy == 1){
yield break;
}
}
}
IEnumerator FadeOut(){
while(true){
Camera.main.GetComponent<$$anonymous$$ino.Datamosh>().entropy = $$anonymous$$athf.SmoothStep(1,0,Time.deltaTime * Speed);
if(GetComponent<$$anonymous$$ino.Datamosh>().entropy == 0){
GetComponent<$$anonymous$$ino.Datamosh>().enabled = false;
yield break;
}
}
}
and gives me a end-of-file error
Your answer
Follow this Question
Related Questions
Move camera forward/backwards with mouse wheel 1 Answer
How can I switch between two cameras ? 1 Answer
How can i make a FF Crystal Chronicles Ring of Fates Controller in Unity ? (Final fantasy) 0 Answers
Setting Virtual Camera by Script 0 Answers
How to make camera position relative to a specific target. 1 Answer