Last active
September 20, 2020 17:57
-
-
Save BanksySan/39f3db746b0a29871dd398c9ccb277f4 to your computer and use it in GitHub Desktop.
Unity Target Group weight change
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* 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; | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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