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 raphu604 · Jun 28, 2013 at 02:29 PM · lerpsmoothmathfsmoothdampcrouch

How can I smooth out the crouch movement?

I know this question has been asked before, but I didn't understand any of the answers. My crouch script works, but it is not smooth. The camera just teleports instead of smoothly moving down. Here's my code: (by the way: I cut out the part where I resize the collider and the center of the character controller and so on... for the sake of clearness)

 using UnityEngine;
 using System.Collections;
 
 public class PlayerMovement : MonoBehaviour {
 
     private bool crouch = false;
     
     private CharacterMotor charMotor;
     private CharacterController charCont;
     
     void Start (){
     
         charMotor = GetComponent<CharacterMotor>();
         charCont = GetComponent<CharacterController>();
     }
     // Update is called once per frame
     void Update () 
     {
             if(Input.GetButtonDown("Crouch"))
             {
                 crouch=true;
             
                 Camera.main.transform.localPosition = new Vector3(0, -crouchHeightOffset, 0); 
                 charMotor.movement.maxForwardSpeed -= 3.4f;
                 charMotor.movement.maxBackwardsSpeed -= 3.4f;
                 charMotor.movement.maxSidewaysSpeed -= 3.4f; 
             }

             if (Input.GetButtonUp("Crouch"))
             {
                 crouch=false;
             
                 Camera.main.transform.localPosition = new Vector3(0,0,0);
                 charMotor.movement.maxForwardSpeed += 3.4f;
                 charMotor.movement.maxBackwardsSpeed += 3.4f;
                 charMotor.movement.maxSidewaysSpeed += 3.4f; 
             }
 
         if (crouch)
             charMotor.jumping.enabled=false; else charMotor.jumping.enabled=true;
         
     } 
     
     
 }

I tried Mathf.Lerp, Mathf.SmoothDamp, Mathf herpderp, Vector3.Lerp, almost everyting I guess.... I just can't get it to work. How does this Lerp and SmoothDamp work EXACTLY? (Sorry, but I really don't understand S*** of those docs about Lerp and SmoothDamp).

I hope somebody can help me out with this :)

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
2
Best Answer

Answer by TonyLi · Jun 28, 2013 at 03:08 PM

You need to call SmoothDamp() repeatedly until you're within the desired threshold of your goal value.

Try something like this (untested code):

 private float smoothTime = 0.3f; // Smoothly crouch/uncrouch over 0.3 sec.
 private float targetY = 0; // Where we want camera local Y to be.
 private float velocityY = 0; // Velocity toward targetY.
 
 void Update() {
     if (Input.GetButtonDown("Crouch")) {
         crouch=true;
         targetY = -crouchHeightOffset;
         charMotor.movement.maxForwardSpeed -= 3.4f;
         charMotor.movement.maxBackwardsSpeed -= 3.4f;
         charMotor.movement.maxSidewaysSpeed -= 3.4f;
     }
  
     if (Input.GetButtonUp("Crouch")) {
         crouch=false;
         targetY = 0;
         charMotor.movement.maxForwardSpeed += 3.4f;
         charMotor.movement.maxBackwardsSpeed += 3.4f;
         charMotor.movement.maxSidewaysSpeed += 3.4f;
     }
 
     // Smoothly update the camera position one step closer to where we want it.
     float newY = Mathf.SmoothDamp(Camera.main.transform.localPosition.y, targetY, ref velocityY, smoothTime);
     Camera.main.transform.localPosition = new Vector3(0, newY, 0);
 
     charMotor.jumping.enabled = !crouch;
 } 
Comment
Add comment · Show 7 · 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 raphu604 · Jun 28, 2013 at 05:26 PM 0
Share

What happens here is that the camera floats above the player, although when it moves up it's a smooth motion hahaha... Any ideas? :) I still have to grasp how the Lerp and SmoothDamp functions ACTUALLY work...

avatar image TonyLi · Jun 28, 2013 at 07:29 PM 0
Share

Does it go the right direction if you change:

     targetY = -crouchHeightOffset;

to:

     targetY = crouchHeightOffset;


The Lerp() function returns an "average" between two values. You give it a number t between 0 and 1.

  • Lerp(a, b, 0) returns a.

  • Lerp(a, b, 1) returns b.

  • Lerp(a, b, 0.5) returns the value exactly between a & b.

To get a smooth transition using Lerp(), you need to keep track of t yourself and gradually change it. For example, to transition over 10 steps, you could start at t=0. Every step, add 0.1 to t, until you get to t=1.


SmoothDamp() basically keeps track of t for you, in the form of a velocity value. It uses what's known as an easing function to get you from one value to another. As your current value gets closer to the target value, the velocity starts to slow down.

avatar image raphu604 · Jun 28, 2013 at 10:28 PM 0
Share

Thanks a lot for your reply :D it works now, and thanks a lot for the explanations also. They should end up in the docs in my opinion...

avatar image TonyLi · Jun 29, 2013 at 12:20 AM 0
Share

Thanks! Glad I could help out.

avatar image fernandovt · Nov 05, 2013 at 01:12 AM 0
Share

raphu604, could you please show me how your script looked at the end?, if you still have it?, i'm having difficulties with my crouch script too:)

Show more comments

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

17 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

Related Questions

Camera zoom smoothing 1 Answer

SmoothDamp smoothTime ignored! 1 Answer

How you change another script value smoothly on trigger 0 Answers

moving between two points over time 1 Answer

Zoom Out depending on velocity/speed 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