This session is made for anyone to override their camera position, the panel you may visualize in video will look like this:
For programmers:
How to override the position related to orbit camera idea position.
CameraBot will need the the following information,
here is the interface to implement your code.
Interface: IDeaPosition
public interface IDeaPosition { /// <summary>Each group can only taken a position, the one with highest weight</summary> int GroupNumber { get; } /// <summary>the recommand position will sent to <see cref="CameraPivot"/></summary> Vector3 ToLocalPosition { get; } /// <summary>To define how important of this information is.</summary> float Weight { get; } /// <summary>The information that will displayed on position overrider section.</summary> string ToString(); }
following will provide you the base class to help you implement easier.
base class : IDeaPositionBase.cs
using UnityEngine; using Kit.Extend; using System; namespace CF.CameraBot.Parts { [RequireComponent(typeof(Preset))] public class IDeaPositionBase : MonoBehaviour, IDeaPosition { #region interface [SerializeField] private Preset m_Preset; protected Preset preset { get { return m_Preset; } } [Header("Position overrider")] [SerializeField] private int m_GroupNumber; public int GroupNumber { get { return m_GroupNumber; } } [SerializeField] private Vector3 m_LocalPosition; public Vector3 ToLocalPosition { get { return m_LocalPosition; } protected set { m_LocalPosition = value; } } [SerializeField] [Range(0f,1f)] private float m_Weight = .5f; public float Weight { get { return m_Weight; } } #endregion #region common variables /// <summary>The clone position & rotation related to chase target</summary> /// <remarks>Non-real chase target, it's preset's instance</remarks> protected Transform GetChaseTarget { get { return preset.Instance.transform; } } /// <summary>The offset applied on chase target</summary> protected Transform GetChaseTargetOffset { get { return preset.Instance.GetChaseTargetOffset(); } } /// <summary>The camera pivot point, position & rotation</summary> protected Transform GetCameraCurrentPivot { get { return preset.Instance.GetCameraPivot(); } } /// <summary>The camera pivot point, without modify</summary> protected Transform GetCameraIdeaPivot { get { return preset.Instance.GetCameraOffset(); } } /// <summary>The location, should focus by camera</summary> protected Transform GetCameraLookAt { get { return preset.Instance.GetCameraLookAt(); } } /// <summary>The idea Camera direction</summary> protected Vector3 GetCameraIdeaDirection { get { return GetCameraLookAt.position.Direction(GetCameraIdeaPivot.position); } } /// <summary>The idea Camera distance</summary> protected float GetCameraIdeaDistance { get { return GetCameraLookAt.position.Distance(GetCameraIdeaPivot.position); } } #endregion #region helper protected virtual void OnValidate() { if (m_Preset == null) { m_Preset = GetComponent<Preset>(); } // for debug purpose, not allow to change. m_LocalPosition = Vector3.zero; } protected virtual void OnEnable() { m_Preset.Instance.CameraPivot.RegisterOperator(this); } protected virtual void OnDisable() { m_Preset.Instance.CameraPivot.UnregisterOperator(this); } public override string ToString() { return string.Format("[G:{5}][W:{0:F2}] {1} :: Pos({2:F2},{3:F2},{4:F2})", Weight, GetType().Name, ToLocalPosition.x, ToLocalPosition.y, ToLocalPosition.z, GroupNumber); } #endregion } }
And here is the sample code to achieve the basic backward obstacle avoidance, the one used in demo video.
Sample code : BackwardObstacleAvoidance.cs
using UnityEngine; using Kit.Extend; namespace CF.CameraBot.Parts { [RequireComponent(typeof(Preset))] public class BackwardObstacleAvoidance : IDeaPositionBase, IDeaPosition { public float m_SafeDistance = 0.2f; /// <summary>Ignore nothing by default, recommand ignore chase target's layer</summary> public LayerMask m_IgnoreLayerMask = 1; void FixedUpdate() { BackwardRaycast(); } private void BackwardRaycast() { RaycastHit hit; if (Physics.Raycast( GetCameraLookAt.position, GetCameraIdeaDirection, out hit, GetCameraIdeaDistance, ~m_IgnoreLayerMask, QueryTriggerInteraction.UseGlobal)) { ToLocalPosition = GetCameraIdeaPivot.InverseTransformPoint(hit.point.PointOnDistance(-GetCameraIdeaDirection, m_SafeDistance)); Debug.DrawLine(GetCameraLookAt.position, hit.point, Color.red, 0.01f); } else { ToLocalPosition = Vector3.zero; } } } }
by following the interface to implement your code, we are able to override the orbit camera position to anywhere, problem is how to achieve your ideas.