Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Paliandro · Dec 28, 2020 at 01:46 PM · beginnerlerptransform.position

Smooth Transform with Lerp?

What I'm trying to achieve is having my player perform a sideways dash, as a dodge maneuver. This is the best I've gotten scrambled together (scouring the internet), but I think I've misunderstood the use of lerp or something. The character does move, but it just performs an instant teleportation. In addition the character moves to the left, and a bit diagonally downwards, even though in unity I've only set the target position to -10, 0, 0?

     public class Dodge4 : MonoBehaviour
     {
         public Vector3 targetPosition;
         public float smoothFactor = 10;
         void Update()
         {
             if (Input.GetButtonDown("Jump Left"))
             {
                 transform.position = Vector3.Lerp(transform.position, targetPosition, Time.deltaTime * smoothFactor);
             }
         }
     }
 
Comment
Add comment · Show 2
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 Pokedlg3 · Dec 29, 2020 at 12:43 AM 0
Share

do you want to subtract 10 from position x from the current position or do you want its position to become (-10,0,0)?

There is a big difference between the two, if you intend to make the first option, the y and z position will matter, but the x position will be the current x position -10 and it will not move diagonally downwards. If you want it, the way you are doing it is wrong, but you can use Vector3.Lerp. However, if you want it to go in the direction (-10,0,0), your script is correct, but your object's y and z position is different from 0, so it moves diagonally.

avatar image Paliandro Pokedlg3 · Dec 29, 2020 at 09:05 AM 0
Share

Do you want to subtract 10 from position x from the current position

This

1 Reply

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

Answer by VeryAnnoyingCat · Dec 30, 2020 at 08:24 AM

While my previous answer that I gave at 1am was incorrect, I still believe that updating the start vector of lerp while lerping is a bad idea since it makes the lerp theoretically infinite. Probably use some other kind of smoothing such as a sinusoidal curve?

 public class Test : MonoBehaviour
 {
     private Vector2 startPos; // position from where the player is dashing
     private Vector2 targetPos; // position to where the player is dashing
     public Vector2 dashDistance; // dash distance. Set x to -10, y to 0
     private bool isDashing = false;
     private float timeSinceDash;
     public float dashTime; // time of dash (in seconds)
     private float dashInterpolationCounter;
  
     void Update()
     {
         if (Input.GetKeyDown(KeyCode.Space) && isDashing == false) // prevents dashing during the dash. Set KeyCode.Space to desired button press
         {
             startPos = transform.position; // sets the start position of the dash to the player position
             targetPos += dashDistance;
             isDashing = true;
             timeSinceDash = 0;
             dashInterpolationCounter = 0;
         }
 
         if (timeSinceDash < dashTime) // if the time since start of dash is < the time of dash (ie. dash is unfinished)
         {
             timeSinceDash += Time.deltaTime;
             dashInterpolationCounter = Mathf.Sin(Mathf.PI/2 * timeSinceDash / dashTime); // change it so that it is one at the end of the dash
         }
         else // the time since start of dash = the time of dash (ie. dash is finished)
         {
             isDashing = false; 
         }
 
         if (isDashing == true)
         {
             transform.position = Vector2.Lerp(startPos, targetPos, dashInterpolationCounter);
         }
     }
 }
 
Comment
Add comment · Show 1 · 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 Paliandro · Dec 31, 2020 at 12:22 PM 0
Share

Thanks a lot! This worked for me mostly, the movement was still a bit wonky, so I changed it to make the target position base on the transform position.

 public class Dodge6 : $$anonymous$$onoBehaviour
 {
     private Vector2 startPos; // position from where the player is dashing
     private Vector2 targetPos; // position to where the player is dashing
     public Vector2 dashDistance; // dash distance. Set x to -10, y to 0
     private bool isDashing = false;
     private float timeSinceDash;
     public float dashTime; // time of dash (in seconds)
     private float dashInterpolationCounter;
 
     void Update()
     {
         if (Input.GetButtonDown("Jump Left") && isDashing == false) // prevents dashing during the dash. Set KeyCode.Space to desired button press
         {
             startPos = transform.position; // sets the start position of the dash to the player position
             targetPos.x = transform.position.x -1;
             targetPos.y = transform.position.y;
             isDashing = true;
             timeSinceDash = 0;
             dashInterpolationCounter = 0;
         }
 
         if (Input.GetButtonDown("Jump Right") && isDashing == false) // prevents dashing during the dash. Set KeyCode.Space to desired button press
         {
             startPos = transform.position; // sets the start position of the dash to the player position
             targetPos.x = transform.position.x + 1;
             targetPos.y = transform.position.y;
             isDashing = true;
             timeSinceDash = 0;
             dashInterpolationCounter = 0;
         }
 
         if (timeSinceDash < dashTime) // if the time since start of dash is < the time of dash (ie. dash is unfinished)
         {
             timeSinceDash += Time.deltaTime;
             dashInterpolationCounter = $$anonymous$$athf.Sin($$anonymous$$athf.PI / 2 * timeSinceDash / dashTime); // change it so that it is one at the end of the dash
         }
         else // the time since start of dash = the time of dash (ie. dash is finished)
         {
             isDashing = false;
         }
 
         if (isDashing == true)
         {
             transform.position = Vector2.Lerp(startPos, targetPos, dashInterpolationCounter);
         }
     }
 }

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

129 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

Related Questions

Portal doors and exits. Can you set up one script to handle tranporting of the character to the exit? 1 Answer

How to reverse a sin animation? 2 Answers

object's children not moving with it when using a Lerp-ing coroutine 0 Answers

I'm used to Java/Processing, some help with new() please 2 Answers

transform.position vector3.lerp appears to stay still for a while before the 5DP matches the new location 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