- Home /
Activate camera script with a trigger
The idea is simple. When the player hits the trigger, the camera needs to use the following script for 5 seconds (the trigger can be activated only once). I dont know how to code and that makes this task difficult for me. Any help? Thanks ;D
The script
// Kino/Motion - Motion blur effect
//
// Copyright (C) 2016 Keijiro Takahashi
using UnityEngine;
namespace Kino
{
[RequireComponent(typeof(Camera))]
[AddComponentMenu("Kino Image Effects/Motion")]
public partial class Motion : MonoBehaviour
{
#region Public properties
/// The angle of rotary shutter. The larger the angle is, the longer
/// the exposure time is.
public float shutterAngle {
get { return _shutterAngle; }
set { _shutterAngle = value; }
}
[SerializeField, Range(0, 360)]
[Tooltip("The angle of rotary shutter. Larger values give longer exposure.")]
float _shutterAngle = 270;
/// The amount of sample points, which affects quality and performance.
public int sampleCount {
get { return _sampleCount; }
set { _sampleCount = value; }
}
[SerializeField]
[Tooltip("The amount of sample points, which affects quality and performance.")]
int _sampleCount = 8;
/// The strength of multiple frame blending. The opacity of preceding
/// frames are determined from this coefficient and time differences.
public float frameBlending {
get { return _frameBlending; }
set { _frameBlending = value; }
}
[SerializeField, Range(0, 1)]
[Tooltip("The strength of multiple frame blending")]
float _frameBlending = 0;
#endregion
#region Private fields
[SerializeField] Shader _reconstructionShader;
[SerializeField] Shader _frameBlendingShader;
ReconstructionFilter _reconstructionFilter;
FrameBlendingFilter _frameBlendingFilter;
#endregion
#region MonoBehaviour functions
void OnEnable()
{
_reconstructionFilter = new ReconstructionFilter();
_frameBlendingFilter = new FrameBlendingFilter();
}
void OnDisable()
{
_reconstructionFilter.Release();
_frameBlendingFilter.Release();
_reconstructionFilter = null;
_frameBlendingFilter = null;
}
void Update()
{
// Enable motion vector rendering if reuqired.
if (_shutterAngle > 0)
GetComponent<Camera>().depthTextureMode |=
DepthTextureMode.Depth | DepthTextureMode.MotionVectors;
}
void OnRenderImage(RenderTexture source, RenderTexture destination)
{
if (_shutterAngle > 0 && _frameBlending > 0)
{
// Reconstruction and frame blending
var temp = RenderTexture.GetTemporary(
source.width, source.height, 0, source.format
);
_reconstructionFilter.ProcessImage(
_shutterAngle, _sampleCount, source, temp
);
_frameBlendingFilter.BlendFrames(
_frameBlending, temp, destination
);
_frameBlendingFilter.PushFrame(temp);
RenderTexture.ReleaseTemporary(temp);
}
else if (_shutterAngle > 0)
{
// Reconstruction only
_reconstructionFilter.ProcessImage(
_shutterAngle, _sampleCount, source, destination
);
}
else if (_frameBlending > 0)
{
// Frame blending only
_frameBlendingFilter.BlendFrames(
_frameBlending, source, destination
);
_frameBlendingFilter.PushFrame(source);
}
else
{
// Nothing to do!
Graphics.Blit(source, destination);
}
}
#endregion
}
}
Answer by RedBambooLeaf · Mar 05, 2020 at 07:43 PM
Assuming that by player hits the trigger you mean that the player presses a button (sorry my english is not very good), this script might do what you're looking for. Attach this script wherever you want and make sure you set the serialized visible values in the inspector. This should be working assuming that Motion is not enabled, this script is enabled and no other class is interfering with the two components. (not tested)
public class CameraBlendActivator : MonoBehaviour
{
// You can change these values from the inspector
[SerializeField] private Motion motion = null; // You need to assign the Motion instance you want to control to this field
[SerializeField] private KeyCode key = KeyCode.Space; // By default Space key is the button to press
[SerializeField] private float blendTime = 5f; // By default set to 5 seconds
private float currentBlendTime;
private void Update()
{
var isBlending = motion.enabled;
if (!isBlending)
{
var hasPlayerHitTheTrigger = Input.GetKeyDown(key);
if (hasPlayerHitTheTrigger)
{
motion.enabled = true; // Let's enable the Motion script
}
}
else
{
var areBlendTimeSecondsPassed = currentBlendTime < blendTime;
if (areBlendTimeSecondsPassed)
{
currentBlendTime += Time.deltaTime;
}
else // Please Note: time passed is not EXACTLY blendTime seconds but very close to it
{
motion.enabled = false; // Let's disable the Motion script
enabled = false; // Let's disable this script, so that it cannot enable the Motion script again
}
}
}
}
Assu$$anonymous$$g that by player hits the trigger you mean that the player presses a button
I mean when the player hits the collider (set on "is trigger").
The script should I put on the camera or on the object witch is the trigger?
Roger! So, assu$$anonymous$$g the player has a non-kinematic, non-static rigidbody AND a collider attached AND it is tagged with the tag "Player", this script should do the job (I added /!/ this comment closeby modified parts):
/*!*/ [RequireComponent(typeof(BoxCollider))]
public class CameraBlendActivator : $$anonymous$$onoBehaviour
{
// You can change these values from the inspector
[SerializeField] private $$anonymous$$otion motion = null; // You need to assign the $$anonymous$$otion instance you want to control to this field
/*!*/ [SerializeField] private BoxCollider boxCollider = null; // You need to assign the BoxCollider component to this field
[SerializeField] private $$anonymous$$eyCode key = $$anonymous$$eyCode.Space; // By default Space key is the button to press
[SerializeField] private float blendTime = 5f; // By default set to 5 seconds
private float currentBlendTime;
private void Update()
{
var isBlending = motion.enabled;
if (!isBlending)
{
/*!*/ var hasPlayerHitTheTrigger = !boxCollider.enabled; // Old condition: Input.Get$$anonymous$$eyDown(key);
if (hasPlayerHitTheTrigger)
{
motion.enabled = true; // Let's enable the $$anonymous$$otion script
}
}
else
{
var areBlendTimeSecondsPassed = currentBlendTime < blendTime;
if (areBlendTimeSecondsPassed)
{
currentBlendTime += Time.deltaTime;
}
else // Please Note: time passed is not EXACTLY blendTime seconds but very close to it
{
motion.enabled = false; // Let's disable the $$anonymous$$otion script
enabled = false; // Let's disable this script, since we don't need its functionalities anymore
}
}
}
/*!*/
private void OnTriggerEnter(Collider other)
{
var isPlayer = other.CompareTag("Player"); // You can do this check in several ways. This is not an optimal solution in terms of maintainability and performance.
if (isPlayer)
{
boxCollider.enabled = false;
}
}
}
Hello. I tried your script. In Unity console I have those errors now :D.
Any idea how to solve them? I also saw that you didint used "using UnityEngine;" at the beginnig..