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 Maggiethegsd · Jan 18, 2021 at 02:57 PM · physicsrigidbodylerpfloatmathf.lerp

Is there a way to smoothly transition between two floats, with it slowing down towards the end

So I am making a helicopter sim in Unity, I was wondering, is there a way to smoothly transition between two floats,

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class Helicopter : MonoBehaviour
 {
     public bool engineOn;
 
     public Rigidbody rb;
 
     public float windDrag;
 
     public float upForce;
     public float gravity;
 
     public Animator animator;
 
     public Transform rotor;
 
     public float pitch;
 
     void Start()
     {
         rb.drag = windDrag;
     }
 
     void Update()
     {
         transform.rotation = Quaternion.Euler(pitch, 0f, 0f);
 
         if (Input.GetKeyDown(KeyCode.E))
         {
             engineOn = !engineOn;
         }
 
         if (engineOn)
         {
             if (Input.GetKey(KeyCode.Space))
             {
                 upForce += 40;
             }
             else if (!Input.GetKey(KeyCode.Space) && upForce > 0)
                 upForce -= 30;
 
             if (Input.GetKey(KeyCode.W))
             {
                 float addedPitch = pitch -= .3f;
                 pitch = Mathf.Lerp(pitch, addedPitch, Time.deltaTime);
             }
 
             if (Input.GetKey(KeyCode.S))
             {
                 float addedPitch = pitch += .3f;
                 pitch = Mathf.Lerp(pitch, addedPitch, Time.deltaTime);
             }
 
             
         }
 
         rb.AddForce(rotor.up * upForce);
 
         SetBooleans();
     }
 
     void SetBooleans()
     {
         animator.SetBool("EngineOn", engineOn);
     }
 }
 



Here I wanna transition between the pitch and addedPitch variable, and kind of want it to go that when I when I hold down the W key, the pitch slowly goes up and starts to speed if I hold it long enough, and when I release W key, the incrementation of pitch slows down and eventually comes to a halt, where pitch is static. Mathf.Lerp didnt work well for me. Please help.

Comment
Add comment · Show 1
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 · Jan 18, 2021 at 05:21 PM 0
Share

do you want that while you press the W key, it will increase the pitch infinitely or to some limit? And when you release the W key do you want the variable to decrease to a limit, or does it stop at the value that stopped?

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by jackmw94 · Jan 18, 2021 at 06:14 PM

There are a few issues with this part here (and the same goes for the same code on 'S')

 float addedPitch = pitch -= .3f;
 pitch = Mathf.Lerp(pitch, addedPitch, Time.deltaTime);


The first line takes pitch, reduces it by .3 then sets addedPitch to be the SAME value. So if pitch is 15, we reduce it to 14.7 then set added pitch to also be 14.7.

Given that pitch and added pitch are the same, when we lerp between them we'll always get that same value therefore the lerp is useless.

Also another note is that since you're passing Time.deltaTime which is always 1 divided by framerate, given that the lerp fraction should be between zero and one, it's always going to be effectively zero.

Now for the fix: If W and S just need to increase and decrease the pitch then you'll probably want to specify a maximum, minimum and change speed.
Now you can call:

 if (Input.GetKey(KeyCode.W))
 {
     pitch -= pitchChangeSpeed * Time.deltaTime;
     pitch = Mathf.Clamp(pitch, minimumPitch, maximumPitch);
 }

Then same for S except changing -= to +=.

Also I've noticed that you don't use the pitch value anywhere within this code so unless you have another script that uses pitch then it won't work because it's never used!

Let me know how you get on!

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 jackmw94 · Jan 20, 2021 at 09:14 AM 0
Share

How are you getting on with this problem @$$anonymous$$aggiethegsd ? Let me know if you need a hand implementing this or if this answer solved your problem, could you accept it? :)

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

220 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 avatar image avatar image avatar image avatar image 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

Rotating one axis using Mathf.Lerp 1 Answer

Rigidbody object intersection 0 Answers

If a rigidbody is attached, should you always use AddTorque/AddRelative force for rotation/movement? 1 Answer

Simulator physics - how to get objects to "stick" to each other 0 Answers

How do I stop my player from very slowly sinking into the ground? 2 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