Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by cassiojs · Apr 20, 2020 at 10:46 PM · movementtransformpositionlerpdouble-click

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;
         }
     }
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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);
      }
Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image cassiojs · Apr 20, 2020 at 11:27 PM 0
Share

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.

avatar image bombombambam cassiojs · Apr 21, 2020 at 11:40 AM 0
Share

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.

avatar image cassiojs bombombambam · Apr 21, 2020 at 03:08 PM 0
Share

Thank you very much!! thank you

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

221 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Lerp to where the target was x seconds ago? 0 Answers

Lerp takes me to 0,0,0 2 Answers

Getting Camera to Stop Following Player 1 Answer

What is wrong with this script ? if duration is 10 why the transform is moving in 11 seconds and not 10 ? 1 Answer

make player move in direction it's facing 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges