How to Enable a Light When a Camera Enters a Collider.
I'm creating a VR game and I'm trying to make a light activated when the camera enters a collider. I also want to know how I should configure the collider. I added a collider to the camera, do I need this? I also want to know if I should have the collider enabled or disable. And should the Trigger on the collider be enabled.
Answer by Stefan02 · Aug 20, 2016 at 10:11 AM
Use Activate Trigger script and put it on collider that you are going to trigger with camera. Camera must have enabled collider that is not trigger. When you make script put it on collider set source with gameobject you put script on, and in target select gameobject that you want to activate.
using System;
using UnityEngine;
using Object = UnityEngine.Object;
namespace UnityStandardAssets.Utility
{
public class ActivateTrigger : MonoBehaviour
{
// A multi-purpose script which causes an action to occur when
// a trigger collider is entered.
public enum Mode
{
Trigger = 0, // Just broadcast the action on to the target
Replace = 1, // replace target with source
Activate = 2, // Activate the target GameObject
Enable = 3, // Enable a component
Animate = 4, // Start animation on target
Deactivate = 5 // Decativate target GameObject
}
public Mode action = Mode.Activate; // The action to accomplish
public Object target; // The game object to affect. If none, the trigger work on this game object
public GameObject source;
public int triggerCount = 1;
public bool repeatTrigger = false;
private void DoActivateTrigger()
{
triggerCount--;
if (triggerCount == 0 || repeatTrigger)
{
Object currentTarget = target ?? gameObject;
Behaviour targetBehaviour = currentTarget as Behaviour;
GameObject targetGameObject = currentTarget as GameObject;
if (targetBehaviour != null)
{
targetGameObject = targetBehaviour.gameObject;
}
switch (action)
{
case Mode.Trigger:
if (targetGameObject != null)
{
targetGameObject.BroadcastMessage("DoActivateTrigger");
}
break;
case Mode.Replace:
if (source != null)
{
if (targetGameObject != null)
{
Instantiate(source, targetGameObject.transform.position,
targetGameObject.transform.rotation);
DestroyObject(targetGameObject);
}
}
break;
case Mode.Activate:
if (targetGameObject != null)
{
targetGameObject.SetActive(true);
}
break;
case Mode.Enable:
if (targetBehaviour != null)
{
targetBehaviour.enabled = true;
}
break;
case Mode.Animate:
if (targetGameObject != null)
{
targetGameObject.GetComponent<Animation>().Play();
}
break;
case Mode.Deactivate:
if (targetGameObject != null)
{
targetGameObject.SetActive(false);
}
break;
}
}
}
private void OnTriggerEnter(Collider other)
{
DoActivateTrigger();
}
}
}
Your answer
Follow this Question
Related Questions
Same script on two objects, only first one works 1 Answer
First person camera flipping by looking up or down too far. 1 Answer
Custom Camera Script Toggle Enabled / Disabled 2 Answers
Script able to target two cameras? 0 Answers
Camera zoom out relative to player scale (using CinemachineFreeLook) 1 Answer