title: how to make a axe throw and return sistem in unity
body: <p>guys,</p>
  <p>so, i'm trying to make a game similar to the god of war in the unit, but i don't know how to make the ax return dynamically, eustou trying to make it come back without problems, but i still couldn't get a satisfactory result, i'm using an Animation to make the ax rotates and when it gets close enough to the player, the script deactivates the Animator and a Quaternion.Slerp Corrects the rotation of the ax, This is the code:</p>
  <pre><code>using System.Collections;
  using System.Collections.Generic;
  using UnityEngine;
  
  
  
  public class AxeController : MonoBehaviour {
  
      // The axe object
      public Rigidbody axe;
      // Amount of force to apply when throwing
      public float throwForce = 50;
      // the target; which is the player's hand.
      public Transform target;
      // The middle point between the axe and the player's hand, to give it a curve
      public Transform curve_point;
      public Transform hand;
      // Last position of the axe before returning it, to use in the Bezier Quadratic Curve formula
      private Vector3 old_pos;
      // Is the axe returning? To update the calculations in the Update method
      private bool isReturning = false;
      // Timer to link to the Bezier formual, Beginnning = 0, End = 1
      public float time = 0.0f;
      public float RotationSpeed;
      public float MinDistance;
      public float Distance;
      public bool hasAxe = true;
      public bool debounce = true;
      public float gunDelay;
      [Range (-10,10)]
      public float TimeReduction;
      [Range (0.1f , 100)]
      public float VelocidadeDeAjuste;
  
      // Update is called once per frame
      void Update () {
  
          Distance = Vector3.Distance(target.position, axe.transform.position);
          // When the user/player hits the mouse left button
          if (Input.GetButtonUp(&quot;Fire1&quot;) &amp;&amp; hasAxe == true &amp;&amp; debounce == true)
          {
              ThrowAxe();
              debounce = false;
              Invoke(&quot;debouncer&quot; , gunDelay);
          }
  
          // When the user/player hits the mouse right button
          if (Input.GetButtonUp(&quot;Fire1&quot;) &amp;&amp; hasAxe == false &amp;&amp; debounce == true)
          {
              ReturnAxe();
              debounce = false;
              Invoke(&quot;debouncer&quot; , gunDelay);
          }
  
          // If the axe is returning
          if(isReturning){
              // Returning calcs
              // Check if we haven't reached the end point, where time = 1
  
  
  
  
                  if(time &lt; 1.0f){
                  // Update its position by using the Bezier formula based on the current time
                  axe.position = getBQCPoint(time, old_pos, curve_point.position, target.position);
                  // Reset its rotation (from current to the targets rotation) with 50 units/s
                  if (Distance &gt; MinDistance) {
                  //axe.transform.localEulerAngles += Vector3.forward * Time.deltaTime;
                  Debug.Log(&quot;Nao Esta Distancia&quot;);
                  }
  
                  if (Distance &lt;= MinDistance) {
                  axe.rotation = Quaternion.Slerp(axe.transform.rotation, target.rotation,Time.deltaTime * (time * VelocidadeDeAjuste));
                  axe.GetComponent&lt;Animator&gt;().enabled = false;
                  Debug.Log(&quot;Em Distancia&quot;);
                  }
                  else
                  {
                      axe.GetComponent&lt;Animator&gt;().enabled = true;
                  }
  
                  // Increase our timer, if you want the axe to return faster, then increase &quot;time&quot; more
                  // With something like:
                  // time += Timde.deltaTime * 2;
                  // It will return as twice as fast
                  time += Time.deltaTime/TimeReduction;
              }else{
                  // Otherwise, if it is 1 or more, we reached the target so reset
                  ResetAxe();
              }
          }
      }
  
      // Throw axe
      void ThrowAxe(){
          // The axe isn't returning
          isReturning = false;
          hasAxe = false;
          // Deatach it form its parent
          axe.transform.parent = null;
          // Set isKinematic to false, so we can apply force to it
          axe.isKinematic = false;
          // Add force to the forward axis of the camera
          // We used TransformDirection to conver the axis from local to world
          axe.AddForce(Camera.main.transform.TransformDirection(Vector3.forward) * throwForce, ForceMode.Impulse);
          // Add torque to the axe, to give it much cooler effect (rotating)
          axe.AddTorque(axe.transform.TransformDirection(Vector3.forward) * RotationSpeed, ForceMode.Impulse);
          axe.transform.GetComponent&lt;AxeWeapon&gt;().Retornando = false;
      }
  
      // Return axe
      void ReturnAxe(){
          // We are returning the axe; so it is in its first point where time = 0
          time = 0.0f;
          // Save its last position to refer to it in the Bezier formula
          old_pos = axe.position;
          // Now we are returning the axe, to start the calculations in the Update method
          isReturning = true;
          // Reset its velocity
          axe.velocity = Vector3.zero;
          // Set isKinematic to true, so now we control its position directly without being affected by force
          axe.isKinematic = true;
          axe.AddTorque(axe.transform.TransformDirection(Vector3.forward * -1) * RotationSpeed, ForceMode.Impulse);
          axe.transform.GetComponent&lt;AxeWeapon&gt;().Retornando = true;
      }
  
      // Reset axe
      void ResetAxe(){
          // Axe has reached, so it is not returning anymore
          isReturning = false;
          // Attach back to its parent, in this case it will attach it to the player (or where you attached the script to)
          axe.transform.parent = transform;
          // Set its position to the target's
          axe.position = target.position;
          // Set its rotation to the target's
          axe.rotation = target.rotation;
          hasAxe = true;
          axe.transform.GetComponent&lt;AxeWeapon&gt;().Retornando = false;
      }
  
      // Bezier Quadratic Curve formula
      // Learn more:
      // https://en.wikipedia.org/wiki/B%C3%A9zier_curve
      Vector3 getBQCPoint(float t, Vector3 p0, Vector3 p1, Vector3 p2){
          // &quot;t&quot; is always between 0 and 1, so &quot;u&quot; is other side of t
          // If &quot;t&quot; is 1, then &quot;u&quot; is 0
          float u = 1 - t;
          // &quot;t&quot; square
          float tt = t * t;
          // &quot;u&quot; square
          float uu = u * u;
          // this is the formula in one line
          // (u^2 * p0) + (2 * u * t * p1) + (t^2 * p2)
          Vector3 p = (uu * p0) + (2
           * u * t * p1) + (tt * p2);
          return p;
      }
  
      void debouncer()
      {
        debounce = true;
      }
  }
  
  
  enter code here
  </code></pre>
  <p>I currently have this:</p>
  <p><a href="https://youtu.be/cEYYnGo-4dg" rel="nofollow noreferrer">https://youtu.be/cEYYnGo-4dg</a></p>