• 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 OctoSloths · Feb 05, 2015 at 11:56 PM · c#vector3transform.positionheightyaxis

Y position keeps changing, after action is complete?

So I have this script that causes the player to duck on a timer when they hit the down key. I want the player to go back to it's original height when the timer is up, the player goes back to close to it's original height but instead the height is dragged down a little each time the action is complete. For example if the starting Y position was 1 and the player ducked, after the timer is up the Y position becomes 0.8 instead of 1. Here is my script:

 public class CharacterDuck : MonoBehaviour {
     public float countDown = 2;//this count down will be used to determine when the character comes back up
     public bool goDown;
     public float startHeight;
     public Vector3 startPos;
     
     void Start()
     {
         startHeight = transform.position.y;
     }
     
     void Update()
     {
         startPos = new Vector3(transform.position.x, startHeight, transform.position.z);
         if (Input.GetKeyDown ("down"))
         {
             goDown = true;
         }
         
         if (goDown)
         {
             countDown -= Time.deltaTime;//count down the countDown to 0
             if (countDown > 1)
             {
                 transform.position = Vector3.Lerp (transform.position, (startPos + new Vector3(0, -5, 0)), Time.deltaTime*2);//use lerp to move the character from their current position to the "down" position
             }
             if (countDown <= 1)
             {
                 transform.position = Vector3.Lerp (transform.position, startPos, Time.deltaTime*2);//use lerp to move the character back to the idle position
                 if (countDown <= 0)
                 {
                     countDown = 2;//reset the cooldown
                     goDown = false;//turn goDown to false
                 }
             }
         }
     }
     
 }

Comment
Add comment · Show 5
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 Superrodan · Feb 06, 2015 at 12:06 AM 0
Share

I think the problem is a combination of using Linear Interpolation and time to cut off your animation and reverse it before it reaches its destination. Linear Interpolation slows down as it approaches its destination.

When you leave your start point the first time to go to your "crouch" destination the destination is far away.. This means your Lerp should be moving fast.

On the way back, however, you will definitely be closer your destination (the original starting point) so it will slow down and not manage to reach the final spot.

This is just a guess

avatar image DanSuperGP · Feb 06, 2015 at 12:18 AM 0
Share

". Linear Interpolation slows down as it approaches its destination."

It shouldn't... it should be LINEAR as in a straight line. It should not have ease in or ease out.

avatar image Superrodan · Feb 06, 2015 at 12:50 AM 0
Share

When you're using Lerp, Linear interpolation finds the halfway point between your current location and your destination.

When you move to a point created by Lerp every frame, its only going halfway to your destination every frame.

As you apprach your destination the halfway points are going to be less far apart, so you will appear to slow down.

I think of it this way: If I am trying to lerp to 1 from 0, I will move 0.5 the first frame, then 0.25 the second frame, then 0.125 the third frame and so on. Using this for movement means the closer I get to my target the slower I appear to be moving.

avatar image DanSuperGP · Feb 06, 2015 at 12:53 AM 0
Share

Oh, right... I see what you mean... he's feeding the position into the lerp every frame...

Weird.

avatar image OctoSloths · Feb 06, 2015 at 01:45 AM 0
Share

This fixed the issue thank you so much! :)

1 Reply

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

Answer by Superrodan · Feb 06, 2015 at 01:01 AM

After reading a bit more about Lerp it's not the halfway point so much as the point represented by the third variable in the parenthesis. It's only halfway if you put 0.5 in.

Still, I still believe that is the issue. I'd suggest going with MoveTowards instead of Lerp. MoveTowards will be a constant value of movement and will behave the same way both on the way down and on the way back.

http://docs.unity3d.com/ScriptReference/Vector3.MoveTowards.html

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

20 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

Related Questions

Multiple Cars not working 1 Answer

How do I get one object to move to (the closet) another object? 2 Answers

How do I make and object's position and rotation independent again? 1 Answer

Distribute terrain in zones 3 Answers

Adding rotation to Physics.AddForce(); ... Getting weird error? 0 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