title: How can I make turret rotate facing target if the target is close enough and make the target rotate around turret with random height and speed?
body: <p>The target is a simple 3d cube.</p>
  
  <p>This screenshot showing the turret in the hierarchy and the script attached to it and the target.</p>
  
  <p><a href="https://i.stack.imgur.com/SQGL5.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/SQGL5.jpg" alt="Turret"></a></p>
  
  <p>The script is attached to the Turret child :</p>
  
  <pre><code>using System.Collections;
  using System.Collections.Generic;
  using UnityEngine;
  
  public class RotateTurret : MonoBehaviour
  {
      [SerializeField]
      private float turnRateRadians = 2 * Mathf.PI;
  
      [SerializeField]
      private Transform turretTop; // the gun part that rotates
  
      [SerializeField]
      private Transform bulletSpawnPoint;
  
      //private Enemy target;
      public GameObject target;
  
      void Update()
      {
          TargetEnemy();
      }
  
      void TargetEnemy()
      {
          if (target != null)
          {
              Vector3 targetDir = target.transform.position - transform.position;
              // Rotating in 2D Plane...
              targetDir.y = 0.0f;
              targetDir = targetDir.normalized;
  
              Vector3 currentDir = turretTop.forward;
  
              currentDir = Vector3.RotateTowards(currentDir, targetDir, turnRateRadians * Time.deltaTime, 1.0f);
  
              Quaternion qDir = new Quaternion();
              qDir.SetLookRotation(currentDir, Vector3.up);
              turretTop.rotation = qDir;
          }
      }
  }
  </code></pre>
  
  <p>When running the game the cube is not moving the turret is not moving. Only if I'm moving the target cube in the scene view window drag it then the turret rotate but also the turret is a bit behind never facing the target cube.</p>
  
  <p>What I need it to do is when running the game the cube will start make circles nonstop around the turret with random speed and the target cube also should move up down with random height between min/max height.</p>
  
  <p>And the turret should rotate facing the target according to the target height and speed.</p>