1
\$\begingroup\$

I will change my question with more details to make it more clear:

  1. In the Assets I created Animator Controller and Animation: Named them both FPSController. FPSController is the object I want to animate it's rotation on the Z from 50 to 0:

Animator Controller and Animation

  1. Then I opened the Animator window and dragged the Animation into the Animator window:

Animator window

  1. Then I opened the Animation window and recorded the clip: The rotation on Z is going from 50 to 0 in 7 seconds. I'm rotating the FPSController on the Z from 50 to 0:

Animation window

This is the Animation part and this is working fine. The FPSController(player) is starting when the Z rotation value is at 50 and slowly move to 0.

Now The game is start when the whole camera is blurry. For this I'm using Post Processing. Next step was to create a profile of the Post Processing called it CC and this profile I dragged in the Hierarchy to the FPSCamera:

CC Post Processing

In the FPSCamera I dragged to it a script name Post Processing Behaviour and then dragged to the script in the Inspector the CC profile: This way the Post Processing effects affecting the camera view:

FPSCamera

So to make it clear: I'm rotating on the Z the FPSController by animation. And on the FPSCamera I'm using the Post Processing for making effects.

This is how the game start. This is the game view screenshot. The game start when everything is blurry and the FPSController(player) is rotating to the right by 50 degrees:

Game view

Now the part of the animation of rotating the FPSController by 50 is working fine. While it's rotating the FPSController I want slowly by the same time in this case 7 seconds to change the blurry effect and make the picture clear once the animation end. The property of the Post Processing CC profile I need to change is the focalLength. By default I start it with the value of 300. And I need in 7 seconds to change it from 300 to 0.

This is the script I ended with so far: The script is attached to the FPSCamera:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.PostProcessing;

public class DOFControl : MonoBehaviour
{
    public PostProcessingProfile postProcProf;

    public Animator anim;
    private float clipLength;

    private void Start()
    {
        AnimationClip[] clips = anim.runtimeAnimatorController.animationClips;
        foreach (AnimationClip clip in clips)
        {
            clipLength = clip.length;
        }

        InvokeRepeating("incrementSpeed", 300, 0.1f);
    }

    void incrementSpeed()
    {
        var dof = postProcProf.depthOfField.settings;
        dof.focalLength -= 1f;
        postProcProf.depthOfField.settings = dof;
    }
}

I'm getting the animation clip time length 7 seconds. But the part with the InvokeRepeating is not working. I want to incrementSpeed the focalLength down from 300 to 0 by 7 seconds. Not sure if using InvokeRepeating is the right way at all.

\$\endgroup\$
  • \$\begingroup\$ Can you not just animate the property in the animation? \$\endgroup\$ – Ed Marty Jul 8 '18 at 15:49
  • 2
    \$\begingroup\$ If this DOFControl is on the camera (or any GameObject in the hierarchy of the Animator) then its properties can be animated in the same way the camera’s rotation can be animated, just by setting the property value in the inspector on the first and last frames of the same animation clip. \$\endgroup\$ – Ed Marty Jul 8 '18 at 17:04
  • 1
    \$\begingroup\$ Yes, although while you are in record mode, you can just change any values of any component in the inspector and it will add a key frame for that property at the current time in the animation window. If this is what you need I will add this as an answer. \$\endgroup\$ – Ed Marty Jul 8 '18 at 18:01
  • \$\begingroup\$ @EdMarty Yes this is what I need, please add a answer. \$\endgroup\$ – Benzi Avrumi Jul 8 '18 at 18:23
  • \$\begingroup\$ @EdMarty I have edited updated and changed my question to make it more clear of what I want and need. Sorry for the mess, I think now it's more clear. \$\endgroup\$ – Benzi Avrumi Jul 8 '18 at 19:12
1
\$\begingroup\$

you can grab the cliptime with something like this then check against that all within the ienumerator if you wanted

    Animator anim;
    var animationClips = anim.runtimeAnimatorController.animationClips;
    for (int i = 0; i < animationClips.Length; i++)
    {
        if(animationClips[i].name == "AnimationName")
        {
            var clipTime = animationClips[i].length;
            if (clipTime == myEventTime)
            {
                focalLength = 0;
            }
        }
    }

Ok reading through the update. I would use a coroutine to decrease the focalLength over time.

 public FloatParameter focalLength;
 private float increment;
 private void Start()
 {
    focalLength = postProcProf.GetSetting<DepthOfField>().focalLength;
    increment = focalLength.value / clipTime;
    StartCoroutine(DecreaseFocalLength());
 }
 IEnumerator DecreaseFocalLength()
{
    while (Time.time< clipTime)
    {   //so if the focal starts @300 and the clipLength is 7 it will divide 42.8571428ect.. / second then stop
        focalLength.value -= increment;
        if (focalLength.value <= 0) focalLength.value = 1;
        yield return new WaitForSeconds(1);        
    }     
}
\$\endgroup\$
  • \$\begingroup\$ This is working I just wonder if it's possible to make it smoother. I mean that now it's jumping by 42.8571428 but I wonder if it can move by 1 second from 300 to (300-42.8571428) fast enough to look smoother. I mean not to jump to the next increment but to move to the next increment. \$\endgroup\$ – Benzi Avrumi Jul 8 '18 at 22:11
  • 1
    \$\begingroup\$ yes you can just yield return new WaitForSeconds(.5f); or whatever you would like and it will transition quicker instead of each second however that would desync with animation clip so if you wanted to keep consistent you would have to increase the clipspeed as well \$\endgroup\$ – Justin Markwell Jul 8 '18 at 22:16
  • 1
    \$\begingroup\$ I changed the WaitForSeconds to 0.00333333333f and the increment I set it to 1. This is what I wanted by smooth. The increment is by 1 but fast enough to take 7 seconds. \$\endgroup\$ – Benzi Avrumi Jul 8 '18 at 22:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.