Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by unity_5jIexhEPsA4PwQ · Mar 05, 2020 at 06:08 PM · cameratriggerscript.scripting beginnercamera-movement

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
     }
 }
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1

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
             }
         }
 
     }
 }

Comment
Add comment · Show 11 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image unity_5jIexhEPsA4PwQ · Mar 05, 2020 at 08:52 PM 0
Share

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"). alt text

The script should I put on the camera or on the object witch is the trigger?

capture.png (7.9 kB)
avatar image RedBambooLeaf unity_5jIexhEPsA4PwQ · Mar 05, 2020 at 09:43 PM 0
Share

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;
             }
         }
     
     }
 
 

avatar image unity_5jIexhEPsA4PwQ RedBambooLeaf · Mar 07, 2020 at 11:44 AM 0
Share

Hello. I tried your script. In Unity console I have those errors now :D. alt text

Any idea how to solve them? I also saw that you didint used "using UnityEngine;" at the beginnig..

capture22.png (19.5 kB)
Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

232 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to adjust camera position/rotation when player looks up or down? (Third Person Camera) 1 Answer

Problem with the camera, 2D. 0 Answers

What is the difference between gameobject movement speed by velocity and calculating the speed with deltatime and magnitude ? 1 Answer

How can I set up cameras to track the player from room to room? 1 Answer

i dont know what i did wrong, anyhelp please? 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges