title: C# How to make a smooth jump in unity3d without moving the X, towards the nearest object
body: <p>I would like to make a smooth jump towards the nearest cube. I already have a script to detect the closest cube. I want that the X-axis is locked, so only the Y-axis and the Z-axis change when jumping. I would like to use a Jump animation when jumping. I already tried to use Vector3MoveTowards, but that didn't really work well, maybe I didn't use it properly.</p>
  
  <p><strong>Detect nearest cube where the player should jump to (C#)</strong></p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code>void Update()
  {
    FindClosestCube ();
    GameObject closestCube = FindClosestCube ();
    Debug.Log (closestCube);
  }
  
  GameObject FindClosestCube() {
  
    GameObject[] gos;
    gos = GameObject.FindGameObjectsWithTag("cube");
    GameObject closest = null;
    float distance = Mathf.Infinity;
    float position = transform.position.z;
    foreach (GameObject go in gos) {
     float diff = go.transform.position.z - position;
  
     float curDistance = diff;
     if (curDistance &lt; distance) {
      closest = go;
      distance = curDistance;
     }
    }
    return closest;
   }</code></pre>
  </div>
  </div>
  </p>
  
  <p>The tricky part is that at some cubes you have to jump up (y+1), with some cubes you jump towards the same Y (y+0) and with some cubes you jump down (y-1).
  How do I do this?</p>
  
  <p><strong>Image of how it looks like:</strong></p>
  
  <p><a href="https://i.gyazo.com/e10f09aa103227c097aef1b36e16b613.gif" rel="nofollow noreferrer"><img src="https://i.gyazo.com/e10f09aa103227c097aef1b36e16b613.gif" alt="enter image description here"></a></p>
  
  <p>EDIT: I have this code right now:</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code> ----------------C#-----------------
  
      Rigidbody rb;
   public int clicks = 0;
   Vector3 target;
   public Animation jumpAnimation;
   bool jump = false;
   float cubeDiffY;
   bool movePlayer;
   public float smoothTime = 0.3f;
   public float yVelocity = 0.0f;
  
   void Start()
   {
    rb = GetComponent&lt;Rigidbody&gt; ();
   }
  
   void Update () 
   {
    FindClosestCube ();
    GameObject closestCube = FindClosestCube ();
    Debug.Log ("Closestcube = " + closestCube);     
  
    target = closestCube.transform.position + new Vector3 (0f, 0.7f, 0f);
  
    cubeDiffY = target.y - transform.position.y;
    movePlayer = true;
  
    Debug.Log("Cube Difference Y-axis = " + Mathf.Round(cubeDiffY));
  
    if (Input.GetMouseButtonDown (0)) 
    {
     clicks += 1;
  
     jump = true;
     jumpAnimation = gameObject.GetComponent&lt;Animation&gt;();
     //jumpAnimation.Play ();
    }
  
    if (jump == true) 
    {
     Jump ();
    }
   }
  
   void Jump()
   {
  
    float newPosition = Mathf.SmoothDamp (transform.position.y, target.y, ref yVelocity, smoothTime);
    transform.position = new Vector3 (0, newPosition, transform.position.z);
   }</code></pre>
  </div>
  </div>
  </p>
  
  <p>I calculated the difference in Y-axis between the cube where the player is standing on and the closestCube. But the Jump() doesn't work. How do I fix that?</p>