- Home /
 
Add specific movement on double click
I'm sorry if already have an answer to this question here, I searched a lot and tried many things. I just want the object to move smoothly to a specific distance to the right or left when I double click, but it just disappears and appears in the other place. I already tried to use lerp but it didn't work.
 public float laneSpeed;
     public float changeSpeed;
     
     private float lastClickTime;
     private const float doubleClickTime = 0.2f;
     private float h;
     private float v;
     
     // Update is called once per frame
     void Update()
     {
         h = Input.GetAxis("Horizontal");
         v = Input.GetAxis("Vertical");
 
         Vector3 direction = new Vector3(h, v, 0);
 
         transform.position += direction * laneSpeed * Time.deltaTime;
 
         if (Input.GetKeyDown(KeyCode.D))
         {
             float timeSinceLastClick = Time.time - lastClickTime;
             if(timeSinceLastClick <= doubleClickTime)
             {
                 transform.position += new Vector3(10, 0, 0);   //<----
             }
             lastClickTime = Time.time;
         }
         else if (Input.GetKeyDown(KeyCode.A))
         {
             float timeSinceLastClick = Time.time - lastClickTime;
             if (timeSinceLastClick <= doubleClickTime)
             {
                 transform.position += new Vector3(-10, 0, 0);  //<----
             }
             lastClickTime = Time.time;
         }
     }
 
              Answer by bombombambam · Apr 20, 2020 at 10:57 PM
The lerp function has to run each frame to work as you wish, so for example like this:
      public float laneSpeed;
      public float changeSpeed;
      
      private float lastClickTime;
      private const float doubleClickTime = 0.2f;
      private float h;
      private float v;
      Vector3 targetPosition;
      
      // Update is called once per frame
      void Update()
      {
          h = Input.GetAxis("Horizontal");
          v = Input.GetAxis("Vertical");
  
          Vector3 direction = new Vector3(h, v, 0);
  
          transform.position += direction * laneSpeed * Time.deltaTime;
  
          if (Input.GetKeyDown(KeyCode.D))
          {
              float timeSinceLastClick = Time.time - lastClickTime;
              if(timeSinceLastClick <= doubleClickTime)
              {
                  targetPosition = transform.position + new Vector3(10, 0, 0);   //<----
              }
              lastClickTime = Time.time;
          }
          else if (Input.GetKeyDown(KeyCode.A))
          {
              float timeSinceLastClick = Time.time - lastClickTime;
              if (timeSinceLastClick <= doubleClickTime)
              {
                  targetPosition = transform.position + new Vector3(-10, 0, 0);  //<----
              }
              lastClickTime = Time.time;
          }
          transform.position = Vector3.Lerp(transform.position, targetPosition, 0.1);
      }
 
              Thank you very much for answering me. Just one more question, what would be the best way to desactivate the command, since when I double-click it does not stop going to position 10 or -10.
With a boolean
 bool can$$anonymous$$ove;
 if(can$$anonymous$$ove) transform.position = Vector3.Lerp(transform.position, targetPosition, 0.1);
 
                   and you can modify it as you want.
Your answer
 
             Follow this Question
Related Questions
Lerp to where the target was x seconds ago? 0 Answers
Lerp takes me to 0,0,0 2 Answers