Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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
1
Question by StrawHatGuy · Feb 15, 2014 at 05:29 AM · rigidbodyrigidbody2dlerprigidbody.velocity

How to lerp a car while it's moving forward at a constant velocity

This car is a rigidbody2D object that is constantly going Vector3.up * 10 while it does not collide into anything on the 2D plane. While I can move the car left and right, it does not translate smoothly. My objective is to be able to move the car left and right smoothly, but the fact that the car is constantly moving and that I can't just lerp the X-position of the car is causing me a headache.

 void Update () {
         if (Input.GetKeyDown("left"))
         {
             if (transform.position.x > -1)
             {
                 //Vector3 targetPos = transform.position;
                 //targetPos.x -= 1.25f;
                 MoveLeft ();
             }
         }
         if (Input.GetKeyDown("right"))
         {
             if (transform.position.x < 1.5)
             {
                 MoveRight();
             }
         }
     }                               
 
     void MoveLeft() {
         Vector3 targetPos = transform.position;
         targetPos.x -= 1.25f;
         transform.position = Vector3.Lerp(transform.position, targetPos, 1f);
     }
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 whydoidoit · Feb 15, 2014 at 07:31 AM

Use Mathf.Lerp rather than Vector3.Lerp to move just the X position of the car - then make your position a combination of the X you are lerping at the rest of it.

      float xPos = 0;
      float horizontalMovement = 2;

      void MoveLeft() {
          xPos = Mathf.Lerp(xPos, xPos-horizontalMovement, Time.deltaTime * 3);
      }

     void Update() {
         //Move vehicle upwards etc
         ...
         var pos = transform.position;
         pos.x = xPos;
         transform.position  = pos;
     }
Comment
Add comment · Show 4 · 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 StrawHatGuy · Feb 15, 2014 at 06:26 PM 0
Share

I should have been more specific with my objective, so I apologize for the lack of detail in regards to that.

What I'm trying to do is to get the car to change lanes with just one keystroke. Eventually, I'll put the game on a mobile device so it will move into another lane with just one horizontal swipe. It would be simple to do if the car wasn't constantly moving, but the fact that it does makes it difficult to implement a solution.

avatar image whydoidoit · Feb 16, 2014 at 05:04 AM 0
Share

So sure - what you need for that is something like this:

  float xPos;
  float laneWidth = 4;
  float laneChangeSpeed  = 2f;

  void OnEnable() {
      xPos = transform.position.x;
  }

  void Update() {

       if(Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.A)) {
           StartCoroutine($$anonymous$$oveLane(-laneWidth));
       }
       ...
       var pos = transform.position;
       pos.x = xPos;
       transform.position = pos;
  }

  IEnumerator $$anonymous$$oveLane(float offset) {
      var t = 0f;
      while(t < 1) {
           t += Time.deltaTime * laneChangeSpeed;
           xPos = $$anonymous$$athf.Lerp(xPos, xPos+offset, t);
           yield return null;
      }
  }
avatar image StrawHatGuy · Feb 16, 2014 at 05:55 AM 0
Share

Thank you, I will try this code as soon as I get back home.

By the way, am I supposed to adjust the offset after each lerp, or is that not necessary?

avatar image whydoidoit · Feb 16, 2014 at 05:56 AM 0
Share

Nope - the lerp is moving from one position to the other - it will an non-eased change however.

If you want easing we need to apply a function to the t parameter to create that effect or use an Animation curve.

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

21 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

Related Questions

Rigidbody2D adding force vs modifying velocity for character jump 1 Answer

Why does Rigidbody.OnDisable() reset the velocity? 1 Answer

Rigidbody.velocity Not working on parent rigidbody with four child rigidbodies attached to it 1 Answer

Kinematic rigidbody movement. 2 Answers

Recalculating collisions after Physics2D.IgnoreLayerCollision()? 1 Answer


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