Hi,
Can you share a video and code for âHUD to move to the selected unitâ? I am intreaged.
My UI is very simple, Just a grid layout on a panel with buttons.
(I am getting all my function in place before I make things pretty with Blender! )
Not sure how to easily get a video⌠and its quite late, but I can share code for now.
A new script âHUDUnitsâ monitors the OnUnitSpawned/OnUnitDead events. To add the the UI container dynamically.
And other events as needed, when a unit takes damage etcâŚ
On the UI Click handler of each HUD Unit UI element. I start a camera transition using Cinemachine.
The trickier bit, I think is the cinemachine blend/detect aspect.
Essentially, I have just extended the CameraManager class.
And because its using cinemachine, it even points the direction the unit it pointing.
What this also allows is that when its the next turn, we can auto select the first unit and bring into view, whether human or AI.
public class CameraManager : MonoBehaviour
{
[SerializeField] private GameObject actionCameraGameObject;
[SerializeField] private GameObject targetCameraGameObject;
[SerializeField] private Cinemachine.CinemachineVirtualCamera cinemachineVirtualCamera;
[SerializeField] private Cinemachine.CinemachineVirtualCamera cinemachineVirtualCameraTarget;
private Cinemachine.CinemachineBrain cinemachineBrain;
private bool IsMovingToTarget = false;
private bool HasBlendStarted = false;
.......
private void Start()
{
cinemachineBrain = Cinemachine.CinemachineCore.Instance.GetActiveBrain(0);
if(cinemachineBrain==null)
{
Debug.LogError("Could not find active CinemachineBrain");
}
..............................
}
private void Update()
{
if (!IsMovingToTarget) { return; }
if(cinemachineBrain.IsLive(cinemachineVirtualCameraTarget) && !HasBlendStarted && cinemachineBrain.ActiveBlend!=null)
{
Debug.Log("BLEND:" + cinemachineBrain.ActiveBlend);
HasBlendStarted = true;
Debug.Log("Blend started...");
return;
}
else if(HasBlendStarted && cinemachineBrain.ActiveBlend==null) //blending has ended
{
Debug.Log("Blend Done!");
HasBlendStarted = false;
IsMovingToTarget = false;
SetCameraFollowLookAt(cinemachineVirtualCameraTarget, cinemachineVirtualCamera, cameraControllerTransform);
HideTargetCamera();
}
}
private void SetCameraFollowLookAt(Cinemachine.CinemachineVirtualCamera activeVCam, Cinemachine.CinemachineVirtualCamera InactiveVCam,Transform newTransform)
{
Transform activeVCamTransformFollow = activeVCam.Follow;
Transform activeVCamTransformLookAt = activeVCam.LookAt;
cameraControllerTransform.position = activeVCam.Follow.position;
cameraControllerTransform.rotation = activeVCam.Follow.rotation;
}
///This is called from the UI click, to move to the selected unit, smooth transtion
public void TransitionCamera(Transform position)
{
if(targetCameraGameObject!=null)
{
CinemachineTransposer transposer = cinemachineVirtualCamera.GetCinemachineComponent<CinemachineTransposer>();
Vector3 followOffset = transposer.m_FollowOffset;
CinemachineTransposer targetTransposer = cinemachineVirtualCameraTarget.GetCinemachineComponent<CinemachineTransposer>();
targetTransposer.m_FollowOffset = followOffset;
//set new camera lookat/follow targets
cinemachineVirtualCameraTarget.Follow = position;
cinemachineVirtualCameraTarget.LookAt = position;
IsMovingToTarget = true;
ShowTargetCamera();
}
}
Hopefully this helps for now, took me ages googling Cinemachine. Cinemachine does not have a great event when when blending ends. The important bit for me, was setting the new position of the camera controller transform to allow normal camera movement after blending, but from the new position. Took me all evening to this working end-to-end, so not too bad in the end.
Regards,
Paul