/// <summary> /// Update the turret incl. movements and scan for targets /// </summary> public bool Update() { if (Owner == null) { return(false); } Scan(); m_IsFocused = false; m_IsMoving = false; // runs the default behaviour if there is no target if (m_ActiveTarget == null) { if (UseParkPosition) { if (PivotType == MountingPivotType.PivotalPoint) { PivotPoint.rotation = Quaternion.Slerp(PivotPoint.rotation, DefaultPivotRotation, RotationSpeed * Time.deltaTime); if (Quaternion.Angle(PivotPoint.rotation, DefaultPivotRotation) > MaxAngularDeviation) { m_IsMoving = true; } } else if (PivotType == MountingPivotType.SeperateAxes) { if (PivotYawAxis != null) { if (PositionTools.Distance(PivotYawAxis.localEulerAngles, new Vector3(0, DefaultPivotYawRotation, 0)) > MaxAngularDeviation) { PivotYawAxis.localEulerAngles = new Vector3(0, Mathf.LerpAngle(PivotYawAxis.localEulerAngles.y, DefaultPivotYawRotation, RotationSpeed * Time.deltaTime), 0); m_IsMoving = true; } else { PivotYawAxis.localEulerAngles = new Vector3(0, DefaultPivotYawRotation, 0); } } if (PivotPitchAxis != null) { if (PositionTools.Distance(PivotPitchAxis.localEulerAngles, new Vector3(DefaultPivotPitchRotation, 0, 0)) > MaxAngularDeviation) { PivotPitchAxis.localEulerAngles = new Vector3(Mathf.LerpAngle(PivotPitchAxis.localEulerAngles.x, DefaultPivotPitchRotation, RotationSpeed * Time.deltaTime), 0, 0); m_IsMoving = true; } else { PivotPitchAxis.localEulerAngles = new Vector3(DefaultPivotPitchRotation, 0, 0); } } } } } // focus an existing target else { float _height = 1; Collider _collider = m_ActiveTarget.GetComponent <Collider>(); if (_collider != null) { _height = _collider.bounds.size.magnitude / 3; } _height += VerticalTargetAdjustment; Vector3 _target_pos = m_ActiveTarget.position + (Vector3.up * _height); if (PivotType == MountingPivotType.PivotalPoint) { Quaternion _rotation = Quaternion.LookRotation(_target_pos - PivotPoint.position, Vector3.up); PivotPoint.rotation = Quaternion.Slerp(PivotPoint.rotation, _rotation, RotationSpeed * Time.deltaTime); if (Quaternion.Angle(PivotPoint.rotation, _rotation) < MaxAngularDeviation) { m_IsFocused = true; m_IsMoving = false; } else { m_IsFocused = false; m_IsMoving = true; } } else if (PivotType == MountingPivotType.SeperateAxes) { if (PivotYawAxis != null) { Vector3 _yaw_pos = new Vector3(_target_pos.x, PivotYawAxis.position.y, _target_pos.z); Quaternion _yaw_rot = Quaternion.LookRotation(_yaw_pos - PivotYawAxis.position, Vector3.up); PivotYawAxis.rotation = Quaternion.Slerp(PivotYawAxis.rotation, _yaw_rot, RotationSpeed * Time.deltaTime); if (Quaternion.Angle(PivotYawAxis.rotation, _yaw_rot) <= MaxAngularDeviation) { m_IsFocused = true; m_IsMoving = false; } else { m_IsFocused = false; m_IsMoving = true; } } if (PivotPitchAxis != null) { Vector3 _pitch_dir = PivotPitchAxis.position - _target_pos; Vector3 _pitch_hor = new Vector3(_pitch_dir.x, 0, _pitch_dir.z); float _angle = MathTools.NormalizeAngle(360 + Vector3.Angle(_pitch_dir, _pitch_hor) * Mathf.Sign(Vector3.Dot(_pitch_dir, Vector3.up))); PivotPitchAxis.localEulerAngles = new Vector3(Mathf.LerpAngle(PivotPitchAxis.localEulerAngles.x, _angle, RotationSpeed * Time.deltaTime), 0, 0); if (m_IsFocused && PositionTools.Distance(PivotPitchAxis.localEulerAngles, new Vector3(_angle, 0, 0)) <= MaxAngularDeviation) { m_IsFocused = true; m_IsMoving = false; } else { m_IsFocused = false; m_IsMoving = true; } } } } if (m_IsMoving) { MovingSound.Play(); } else { MovingSound.Stop(); } return(m_IsFocused); }
public void PlayMoveingSound() { MovingSound.Position = TimeSpan.Zero; MovingSound.Play(); }