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 /
  • Help Room /
avatar image
0
Question by Lupiyo_Devops · Jun 02, 2020 at 10:09 PM · speednpcpatrol

Patrol unit lose speed when get close to target

Hello everyone, thank you for the support.

I was learning on youtube how to make an 2d RPG, and everything was fine since i tried to make some changes in the code.

I had to implement a NPC that move from A to B, and vice versa. The mechanic of doing this is fine, it goes and back normally. The problem is the speed, i noticed that the longer the npc is from target, the faster it is, and when it gets closer, it gets slow as a snail lol.

I searched on internet and discovered that it is a comum problem, but nothing solved for me.

The code below is inside of FixedUpdate(). I don´t know so much, i'm kind new on unity, so tried everything. Changed to update(), tried moveTowards instead lerp, but i got the same result.

Can someone help me please?! Thank you!!

Obs.: The change goal is a fucntion that changes the target game object only.

        var heading_path = path[current_point].position - this.transform.position;
         heading_path.z = 0;
         var point_distance = heading_path.magnitude;
         var direction_path = heading_path / point_distance; // This is now the normalized direction.
             
         Vector3 temp2 = Vector3.Lerp(this.transform.position, path[current_point].position, 0.005f);             
         Vector3 temp3 = new Vector3(temp2.x, temp2.y, 0);
 
 
         //anda    
         anim.SetBool("wake", false);
         anim.SetBool("walk", true);
         anim.SetBool("attack", false);
         anim.SetBool("sleep", false);
         //temp.z = 16;
         transform.position = temp3;
         Change_anim(direction_path);
         if (point_distance <= 2)
         {
             Change_goal();
         }

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

Answer by KoenigX3 · Jun 03, 2020 at 08:56 AM

Lerp functions take 3 parameters: a start point, an end point, and an interpolant. If the interpolant is 0, the function returns the start point. If the interpolant is 1, the function returns the end point. If the interpolant is between 0 and 1, the function returns a point between the start and end point.

Normally, to make the interpolation linear, you would need a static starting point and a dynamic interpolant. In your case, you are using the Lerp function in a different way.

  1. Your starting point is not static, which means that it changes every frame.

  2. Your interpolant is not dynamic, which means that it is constant.

By doing so, you are achieving a decreasing interpolation. The function returns a point between the start and end point about 0.5% from the start. Since you are using the current position (this.transform.position) as the starting point, the distance between the start and end point decreases, and since you are always returning 0.5% of the distance, it decelerates.

The solution is to use a fixed starting point and an increasing interpolant.

 Vector3 startingPoint;
 float interpolant = 0;
 
 void Update()
 {
    Vector3 temp2 = Vector3.Lerp(startingPoint, path[current_point].position, interpolant);
    interpolant += Time.deltaTime;
 }


Whenever you change the end point, you also have to change the start point to the current position (but only once, of course), and the interpolant to 0.

Comment
Add comment · 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

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

204 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

Related Questions

How to revert speed back to normal after changing with Key. 1 Answer

Something is wrong with my max. speed code 0 Answers

Increase variable? 0 Answers

[SOLVED] When touch and drag objects fast (in android), leave dragging. 1 Answer

Need help, the speed stacks if I press left/up, right/up, left/down or right/down. 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