Skip to content

Instantly share code, notes, and snippets.

@BanksySan
Last active September 20, 2020 17:57
Show Gist options
  • Save BanksySan/39f3db746b0a29871dd398c9ccb277f4 to your computer and use it in GitHub Desktop.
Save BanksySan/39f3db746b0a29871dd398c9ccb277f4 to your computer and use it in GitHub Desktop.
Unity Target Group weight change
/* Blend to camera focus from the character to the firefly.
* No effect on actual target group weighting.
*/
using System.Collections;
using Cinemachine;
using UnityEngine;
namespace Project
{
[RequireComponent(typeof(CinemachineTargetGroup))]
public class TargetGroupBlender : MonoBehaviour
{
private static readonly YieldInstruction _WAIT_FOR_END_OF_FRAME = new WaitForEndOfFrame();
private CinemachineTargetGroup.Target _character;
private CinemachineTargetGroup.Target _firefly;
private CinemachineTargetGroup _targetGroup;
private void Start()
{
_targetGroup = GetComponent<CinemachineTargetGroup>();
_character = _targetGroup.m_Targets[0];
_firefly = _targetGroup.m_Targets[1];
}
public void LerpToFireFly(float duration)
{
StartCoroutine(LerpToFirefly(duration));
}
private IEnumerator LerpToFirefly(float duration)
{
var start = Time.time;
var end = start + duration;
Debug.Log(new {start, end, now = Time.time});
for (var characterWeight = 1f; Time.time < end; characterWeight = (end - Time.time) / duration)
{
Debug.Log($"{nameof(characterWeight)}: {characterWeight:F}");
_character.weight = characterWeight;
_firefly.weight = 1 - characterWeight;
yield return _WAIT_FOR_END_OF_FRAME;
}
}
}
}
using Cinemachine;
using UnityEngine;
namespace Project.Cinemachine_Target_Groups
{
[RequireComponent(typeof(CinemachineTargetGroup))]
public class TargetGroupTransition : MonoBehaviour
{
private CinemachineTargetGroup _targetGroup;
private void Start()
{
_targetGroup = GetComponent<CinemachineTargetGroup>();
}
private void Update()
{
if (Input.GetKeyDown(KeyCode.UpArrow))
{
_targetGroup.m_Targets[0].weight = 0;
_targetGroup.m_Targets[1].weight = 1;
}
if (Input.GetKeyDown(KeyCode.DownArrow))
{
_targetGroup.m_Targets[1].weight = 0;
_targetGroup.m_Targets[0].weight = 1;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment