Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 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
0
Question by b2hinkle · Sep 19, 2018 at 07:09 PM · rotationquaternionlerptime.deltatimeslerp

Lerp 180 degrees while holding B and lerp back 180 degrees when let go of B.

Hey guys. I am trying to make it so that the player turns around 180 degrees (in a lerping fashion) when holding down B and turns back around (also in a lerping fashion) when let go of B. So far the first half works. The player turns around 180 degrees and stays in that direction as long as he holds B, but when he lets go the player instantly goes back to his starting rotation, and doesn't lerp back like I want it to. I don't know what is wrong with my function. (Btw all seemingly undeclared variables are actualy global variables. In case you were wondering.)

 void TurnAroundLerp()
 {
     if (Input.GetButtonUp("B") == false)
     {
         if (Input.GetButtonDown("B"))
         {
             turnAroundStartRotation = transform.rotation;
             turnAroundTargetRotation = Quaternion.Euler(transform.rotation.eulerAngles.x, transform.rotation.eulerAngles.y + 180, transform.rotation.eulerAngles.z);

             turnAroundStartTime = Time.time;
             turnAroundCurrentTime = turnAroundStartTime;
         }
         else if (Input.GetButton("B"))
         {
             turnAroundCurrentTime += Time.deltaTime;
             percentageDone = (turnAroundCurrentTime - turnAroundStartTime) / turnAroundLerpTime;

             if (percentageDone < 1f)
             {
                 transform.rotation = Quaternion.Lerp(turnAroundStartRotation, turnAroundTargetRotation, percentageDone);
             }
             else
             {
                 Quaternion targetRotation = Quaternion.Euler(transform.rotation.eulerAngles.x, transform.rotation.eulerAngles.y + 180, transform.rotation.eulerAngles.z);

                 transform.rotation = targetRotation;
             }
         }
     }
     else if (Input.GetButtonUp("B") == true)
     {
         percentageDone = 0;
         turnAroundStartRotation = transform.rotation;
         turnAroundTargetRotation = Quaternion.Euler(transform.rotation.eulerAngles.x, transform.rotation.eulerAngles.y + 180, transform.rotation.eulerAngles.z);

         turnAroundStartTime = Time.time;
         turnAroundCurrentTime = turnAroundStartTime;

         while (percentageDone < 1)
         {
             Debug.Log(percentageDone);
             turnAroundCurrentTime += Time.deltaTime;
             percentageDone = (turnAroundCurrentTime - turnAroundStartTime) / turnAroundLerpTime;

             transform.rotation = Quaternion.Lerp(turnAroundStartRotation, turnAroundTargetRotation, percentageDone);
         }
     }
     
     


     
 }
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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by kevojo · Sep 19, 2018 at 09:17 PM

There are a lot of things you're doing here that I don't understand why. Why would you check every frame if the button wasn't released before making all those calculations? If the "getButtonUp' was true, there's no way "getButton" or "getButtonDown" could ever be true in the same frame, even if it wasn't embedded under that condition. Also when you say "If (Statement == true)", you don't need to say "else if (statement == false" - simply saying "else" is the same thing when it's a binary condition.

As for rotating the character - I think you might be overthinking this one a little bit (although I don't know how you are controlling rotation passively, so I don't know what you are working around. The simplest way I can think of to do this would be to have two external transforms, one aiming forward, one aiming backwards.

 void AimTransforms (Transform FwdAim, Transform BckAim)
 {
     FwdAim.localEulerAngles = new Vector3(0, mainCamera.transform.y,0);
     BwdAim.LocalEulerAngles = new Vector3(0, mainCamera.Transform.y+180, 0);
 }

Then, use two lerp speeds, one for when the player is changing direction, one for when the player is matching the camera.

 bool ChangingDirection;
 bool  AimForward
 float TurnSpeed;  //the slower speed
 float AimSpeed;   // the faster, almost instantaneous speed
 
 void CheckAimDir()
 {
     if Input.getKey(Keycode.B)
     {
          AimForward = false;
                if (Vector3.angle(player.transform.forward, BckAim, player.transform.up) > 1)
             {
                 ChangingDirection = true;
             }
             else
             {
                 ChangingDirection = false;
             }
     }
     else
     {
         AimForward = true;
          if   (Vector3.angle(player.transform.forward, FwdAim, player.transform.up) > 1)
               {
                 ChangingDirection = true;
                }
             else
             {
                 ChangingDirection = false;
             }
     }
 }


For there, you just have to check the "Changing direction" bool to decide whether to use the fast/instantaneously matching camera lerp, or the slow, turning backwards/forwards lerp.

 void CalculateTurn()
 {
     float Speed;
     if (ChangingDirection)
         {
             Speed = TurnSpeed;
          }
       else
         {
              Speed = AimSpeed;
          }
 
      if (AimForward)
         {
              player.transform.rotation = Quaternion.lerp(player.transform.rotation, FwdAim.rotation, Speed*Time.deltaTime);
         }
      else
         {
               player.transform.rotation = Quaternion.lerp(player.transform.rotation, BwdAim.rotation, Speed*Time.deltaTime);
        }
 }
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
avatar image
0

Answer by Hellium · Sep 19, 2018 at 09:33 PM

 using UnityEngine;
 
 public class Rotate : MonoBehaviour
 {
     public float RotationSpeed = 1;
     public KeyCode RotateKey = KeyCode.B ;
 
     private Quaternion startRotation ;
     private Quaternion endRotation ;
     private Quaternion initialRotation ;
     private Quaternion flippedRotation ;
     private float rotationProgress ;
 
     void Start()
     {
         initialRotation = startRotation = endRotation = transform.rotation ;
         flippedRotation = Quaternion.Euler( 0, 180, 0 ) * transform.rotation ;
         rotationProgress = 1 ;
     }
 
     void Update()
     {
         TurnAroundLerp();
     }
 
     void TurnAroundLerp()
     {
         if( Input.GetKeyDown( RotateKey ) )
         {
             startRotation = initialRotation ;
             endRotation = flippedRotation ;
             rotationProgress = 1 - rotationProgress;
         }
         else if( Input.GetKeyUp( RotateKey ) )
         {
             startRotation = flippedRotation ;
             endRotation = initialRotation ;
             rotationProgress = 1 - rotationProgress;
         }
         
         rotationProgress = Mathf.Clamp01( rotationProgress + RotationSpeed * Time.deltaTime ) ;
         transform.rotation = Quaternion.Slerp( startRotation, endRotation, rotationProgress ) ;
     }
 }
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

126 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

Related Questions

Use Lerp to rotate object 2 Answers

How can I use lerp to rotate an object multiple times? 2 Answers

Transition current camera rotation to 0,0,0 1 Answer

Changing rotation of Quaternion 0 Answers

Quaternion.Slerp rotation speed increases over script life. 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