/// <summary> /// Calculates the camera top parameter position. /// If the topdown mode = Default | ParameterControl , the position is runtime calculated. /// If the topdown mode = PositionAtPivot, the position is directly set to the pivot transform's position. /// </summary> void AlignCameraPosition(bool smoothDamp, TopDownCameraControlParameter topdownCameraControlParameter) { Vector3 CameraPivotPosition = Vector3.zero; Vector3?ParameterCalculatePosition = null; Vector3 CameraLookAtPosition = Vector3.zero; switch (topdownCameraControlParameter.mode) { //note: for Default mode, CameraLookAtPosition IS NOT ASSIGNED ! Because in runtime, we can't focus camera on player, that will shake the camera too much! case TopDownCameraControlMode.Default: ParameterCalculatePosition = CalculateParameterControlPosition(smoothDamp, topdownCameraControlParameter.DynamicHeight, topdownCameraControlParameter.DynamicDistance, topdownCameraControlParameter.smoothLag_Position, GetCharacterCenter()); CameraPivotPosition = ParameterCalculatePosition.Value; CameraLookAtPosition = GetCharacterCenter(); break; case TopDownCameraControlMode.ParameterControlPositionAndLookAtPosition: ParameterCalculatePosition = CalculateParameterControlPosition(smoothDamp, topdownCameraControlParameter.DynamicHeight, topdownCameraControlParameter.DynamicDistance, topdownCameraControlParameter.smoothLag_Position, topdownCameraControlParameter.cameraFocusOnPosition); CameraPivotPosition = ParameterCalculatePosition.Value; CameraLookAtPosition = topdownCameraControlParameter.cameraFocusOnPosition; break; case TopDownCameraControlMode.ParameterControlPositionAndLookAtTransform: ParameterCalculatePosition = CalculateParameterControlPosition(smoothDamp, topdownCameraControlParameter.DynamicHeight, topdownCameraControlParameter.DynamicDistance, topdownCameraControlParameter.smoothLag_Position, topdownCameraControlParameter.cameraFocusOnTransform.position); CameraPivotPosition = ParameterCalculatePosition.Value; CameraLookAtPosition = topdownCameraControlParameter.cameraFocusOnTransform.position; break; case TopDownCameraControlMode.PoisitonAtPivotAndLookAtPlayer: CameraPivotPosition = topdownCameraControlParameter.CameraPositionPivot.position; CameraLookAtPosition = GetCharacterCenter(); break; case TopDownCameraControlMode.PositionAtPivotAndLookAtPosition: CameraPivotPosition = topdownCameraControlParameter.CameraPositionPivot.position; CameraLookAtPosition = topdownCameraControlParameter.cameraFocusOnPosition; break; case TopDownCameraControlMode.PositionAtPivotAndLookAtTransform: CameraPivotPosition = topdownCameraControlParameter.CameraPositionPivot.position; CameraLookAtPosition = topdownCameraControlParameter.cameraFocusOnTransform.position; break; } //finally, asign the position and rotation of the camera. //handle position: CameraPivotPosition = AdjustLineOfSight(CameraPivotPosition, CameraLookAtPosition); transform.position = CameraPivotPosition; }
public void OnGameEvent(GameEvent _event) { switch (_event.type) { case GameEventType.ApplyTopdownCameraParameter: TopDownCameraControlParameter controlParameter = GetTopDownCameraControlParameter(_event.StringParameter); PlayerTopdownCamera.SetCurrentTopdownCameraParameter(controlParameter); break; case GameEventType.ResetTopdownCameraParameter: PlayerTopdownCamera.ResetCurrentTopdownCameraParameter(); break; } }
void AlignCameraRotation(bool smoothDamp, TopDownCameraControlParameter topdownCameraControlParameter) { Vector3?CameraLookAtPosition = null; switch (topdownCameraControlParameter.mode) { //note: for Default mode, CameraLookAtPosition IS NOT ASSIGNED ! Because in runtime, we can't focus camera on player, that will shake the camera too much! case TopDownCameraControlMode.Default: CameraLookAtPosition = GetCharacterCenter(); break; case TopDownCameraControlMode.ParameterControlPositionAndLookAtPosition: CameraLookAtPosition = topdownCameraControlParameter.cameraFocusOnPosition; break; case TopDownCameraControlMode.ParameterControlPositionAndLookAtTransform: CameraLookAtPosition = topdownCameraControlParameter.cameraFocusOnTransform.position; break; case TopDownCameraControlMode.PoisitonAtPivotAndLookAtPlayer: CameraLookAtPosition = GetCharacterCenter(); break; case TopDownCameraControlMode.PositionAtPivotAndLookAtPosition: CameraLookAtPosition = topdownCameraControlParameter.cameraFocusOnPosition; break; case TopDownCameraControlMode.PositionAtPivotAndLookAtTransform: CameraLookAtPosition = topdownCameraControlParameter.cameraFocusOnTransform.position; break; } //handle rotation: if (CameraLookAtPosition.HasValue) { if (smoothDamp) { Quaternion ToRotation = transform.rotation; ToRotation.SetLookRotation(CameraLookAtPosition.Value - transform.position, Vector3.up); transform.rotation = Quaternion.Lerp(transform.rotation, ToRotation, topdownCameraControlParameter.smoothLag_Rotation * Time.deltaTime); transform.rotation = ToRotation; } else { transform.LookAt(CameraLookAtPosition.Value); } } }
/// <summary> /// Applies the camera control parameter to the current camera. /// smoothDamp - if smoothly damp from current camera position ? if false, the camera control parameter takes effect immediately. /// CharacterCenter - the camera center. /// topdownCameraControlParameter - the topDownCameraControlParameter. /// </summary> public virtual void ApplyCameraControlParameter(bool SmoothDamp, TopDownCameraControlParameter topdownCameraControlParameter) { AlignCameraPosition(SmoothDamp, topdownCameraControlParameter); AlignCameraRotation(SmoothDamp, topdownCameraControlParameter); }
public void ResetCurrentTopdownCameraParameter() { this.CurrentTopDownCameraParameter = this.topDownCameraParameter; }
public void SetCurrentTopdownCameraParameter(TopDownCameraControlParameter topdownCameraControlParameter) { this.CurrentTopDownCameraParameter = topdownCameraControlParameter; }
void Awake() { PlayerCharacter = transform.root.GetComponentInChildren <CharacterController> (); CurrentTopDownCameraParameter = topDownCameraParameter; }
/// <summary> /// Applies the camera control parameter to the current camera. /// smoothDamp - if smoothly damp from current camera position ? if false, the camera control parameter takes effect immediately. /// CharacterCenter - the camera center. /// topdownCameraControlParameter - the topDownCameraControlParameter. /// </summary> public virtual void ApplyCameraControlParameter(bool SmoothDamp, TopDownCameraControlParameter topdownCameraControlParameter) { AlignCameraPosition (SmoothDamp, topdownCameraControlParameter); AlignCameraRotation (SmoothDamp, topdownCameraControlParameter); }
void Awake() { PlayerCharacter = transform.root.GetComponentInChildren<CharacterController> (); CurrentTopDownCameraParameter = topDownCameraParameter; }
void AlignCameraRotation(bool smoothDamp, TopDownCameraControlParameter topdownCameraControlParameter) { Vector3? CameraLookAtPosition = null; switch (topdownCameraControlParameter.mode) { //note: for Default mode, CameraLookAtPosition IS NOT ASSIGNED ! Because in runtime, we can't focus camera on player, that will shake the camera too much! case TopDownCameraControlMode.Default: CameraLookAtPosition = GetCharacterCenter(); break; case TopDownCameraControlMode.ParameterControlPositionAndLookAtPosition: CameraLookAtPosition = topdownCameraControlParameter.cameraFocusOnPosition; break; case TopDownCameraControlMode.ParameterControlPositionAndLookAtTransform: CameraLookAtPosition = topdownCameraControlParameter.cameraFocusOnTransform.position; break; case TopDownCameraControlMode.PoisitonAtPivotAndLookAtPlayer: CameraLookAtPosition = GetCharacterCenter (); break; case TopDownCameraControlMode.PositionAtPivotAndLookAtPosition: CameraLookAtPosition = topdownCameraControlParameter.cameraFocusOnPosition; break; case TopDownCameraControlMode.PositionAtPivotAndLookAtTransform: CameraLookAtPosition = topdownCameraControlParameter.cameraFocusOnTransform.position; break; } //handle rotation: if(CameraLookAtPosition.HasValue) { if(smoothDamp) { Quaternion ToRotation = transform.rotation; ToRotation.SetLookRotation(CameraLookAtPosition.Value - transform.position, Vector3.up); transform.rotation = Quaternion.Lerp(transform.rotation, ToRotation, topdownCameraControlParameter.smoothLag_Rotation * Time.deltaTime); transform.rotation = ToRotation; } else { transform.LookAt (CameraLookAtPosition.Value); } } }
/// <summary> /// Calculates the camera top parameter position. /// If the topdown mode = Default | ParameterControl , the position is runtime calculated. /// If the topdown mode = PositionAtPivot, the position is directly set to the pivot transform's position. /// </summary> void AlignCameraPosition(bool smoothDamp, TopDownCameraControlParameter topdownCameraControlParameter) { Vector3 CameraPivotPosition = Vector3.zero; Vector3? ParameterCalculatePosition = null; Vector3 CameraLookAtPosition = Vector3.zero; switch (topdownCameraControlParameter.mode) { //note: for Default mode, CameraLookAtPosition IS NOT ASSIGNED ! Because in runtime, we can't focus camera on player, that will shake the camera too much! case TopDownCameraControlMode.Default: ParameterCalculatePosition = CalculateParameterControlPosition (smoothDamp, topdownCameraControlParameter.DynamicHeight, topdownCameraControlParameter.DynamicDistance, topdownCameraControlParameter.smoothLag_Position, GetCharacterCenter ()); CameraPivotPosition = ParameterCalculatePosition.Value; CameraLookAtPosition = GetCharacterCenter(); break; case TopDownCameraControlMode.ParameterControlPositionAndLookAtPosition: ParameterCalculatePosition = CalculateParameterControlPosition (smoothDamp, topdownCameraControlParameter.DynamicHeight, topdownCameraControlParameter.DynamicDistance, topdownCameraControlParameter.smoothLag_Position, topdownCameraControlParameter.cameraFocusOnPosition); CameraPivotPosition = ParameterCalculatePosition.Value; CameraLookAtPosition = topdownCameraControlParameter.cameraFocusOnPosition; break; case TopDownCameraControlMode.ParameterControlPositionAndLookAtTransform: ParameterCalculatePosition = CalculateParameterControlPosition (smoothDamp, topdownCameraControlParameter.DynamicHeight, topdownCameraControlParameter.DynamicDistance, topdownCameraControlParameter.smoothLag_Position, topdownCameraControlParameter.cameraFocusOnTransform.position); CameraPivotPosition = ParameterCalculatePosition.Value; CameraLookAtPosition = topdownCameraControlParameter.cameraFocusOnTransform.position; break; case TopDownCameraControlMode.PoisitonAtPivotAndLookAtPlayer: CameraPivotPosition = topdownCameraControlParameter.CameraPositionPivot.position; CameraLookAtPosition = GetCharacterCenter(); break; case TopDownCameraControlMode.PositionAtPivotAndLookAtPosition: CameraPivotPosition = topdownCameraControlParameter.CameraPositionPivot.position; CameraLookAtPosition = topdownCameraControlParameter.cameraFocusOnPosition; break; case TopDownCameraControlMode.PositionAtPivotAndLookAtTransform: CameraPivotPosition = topdownCameraControlParameter.CameraPositionPivot.position; CameraLookAtPosition = topdownCameraControlParameter.cameraFocusOnTransform.position; break; } //finally, asign the position and rotation of the camera. //handle position: CameraPivotPosition = AdjustLineOfSight (CameraPivotPosition, CameraLookAtPosition); transform.position = CameraPivotPosition; }