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 TheWhaleDev · Mar 11, 2014 at 02:24 AM · rotationlerp

Problem With Mathf.Lerp

okay im trying to get my camera to lerp to an objects position.....i move the camera using this code....gameObject.transform.Rotate(Vector3(recoil, 0, 0));....so my camera is moved now im trying to get it to lerp back to another objects position....using this code...

 var cameraToLerp : GameObject;
 
 var lerpTime : float = 1;
 
 function Update () 
 {
 
     this.gameObject.transform.rotation.x = Mathf.Lerp(this.gameObject.transform.rotation.x, cameraToLerp.transform.rotation.x, lerpTime);
 }


and the camera doesnt move at all..the camera moves when i comment out the lerp command...but i dont know how to solve this and there are no tutorials that work.

Comment
Add comment · Show 3
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 robertbu · Mar 11, 2014 at 03:21 AM 0
Share

There are serious issues here, and without more context I cannot tell you how to solve your problems. Transform.rotation is Quaternion...an non-intuitive 4D construct with values between -1 and 1. Don't manipulate the individual components unless you have a firm understanding of the math behind Quaternions. You can use Transform.eulerAngles ins$$anonymous$$d, but the manual states:

Do not set one of the eulerAngles axis separately (eg. eulerAngles.x = 10; ) since this will lead to drift and undesired rotations.

In addition to the above problem, there are multiple eulerAngle representations for any give physical rotation, and Unity can (and does) change representation on you.

As for your Lerp() there are multiple different ways to use Lerp() and other functions like $$anonymous$$oveTowards() that can be used ins$$anonymous$$d. What you are doing here is not one of them. The last parameter either needs to vary from 0 to 1 over time, or it needs to be a small fraction. Typically 'speed * Time.deltaTime' is used and produces and eased rotation.

I'm sure there is an easy way to get what you want, but to do so I think you need to backup and describe in detail what this code needs to do...what is the relationship between the rotation of this game object and the cameraToLerp object.

avatar image TheWhaleDev · Mar 11, 2014 at 02:09 PM 0
Share

All im trying to accomplish in this code is for the object to return back to the same rotation value as the other object...like the x,y,z rotation values....i need my object to return to those values over a certain period of time...im just having trouble finding the right tutorial for my problem.

avatar image TheWhaleDev · Mar 11, 2014 at 03:12 PM 0
Share

never$$anonymous$$d i figured it out by putting time.deltatime for t...it worked just as i hoped...thanks anyways for trying to help me

2 Replies

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

Answer by Lairinus · Mar 11, 2014 at 03:22 AM

Going off of what Ben said, it seems you don't really understand what Lerp does.

I'll try my best to explain it, as I didn't fully understand it until recently.

When people talk of Lerping something, they mean to move an object from point A to point B over t Time, where t DOES NOT equal the actual "arrival time." t Will NEVER equal the arrival time, and will always be between 0 and 1. 0 is the start, and 1 is the end. (You need division here)

I'll use "arrival time" a lot in my explanation, but just know that arrival time means the total time that you want the number to go from its' start to its' finish (or its' 0 to 1)

I'll post my gravity script as an example:

     IEnumerator Lerp_CurrentGravity()
     {
         _gravity.currentGravity = 0;
 
         float totalTimeDelta = 0;
         // Gradually Adjust Gravity
         while (_gravity.currentGravity < _gravity.maxGravity && GetGravityState() == Gravity.State.Falling)
         {
             totalTimeDelta += Time.deltaTime;
             _gravity.currentGravity = Mathf.Lerp(_gravity.currentGravity, _gravity.maxGravity, totalTimeDelta / _gravity.timeForMaxGravity);        // Lerps the gravity
             yield return new WaitForSeconds(Time.deltaTime);
         }
 
         if (GetGravityState() != Gravity.State.Falling)
             _gravity.currentGravity = 0;
     }

Look inside the While loop and you'll see a totalTimeDelta float. That represents the total time that you've been "falling" in this instance of my script.

Now if you know that your actual "arrival" time is 2, then you'll need to do the operation:

(totalTimeDelta / 2), which will give you an answer between 0 and 1.

This works for every sort of Lerping because its' all based around the same principal.

WHAT YOU'RE DOING:

You're setting the Lerp value directly to 1, which, is exactly the same as saying "Finish Lerping." You might as well use transform.rotation = new Quaternion(x) at that point.

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 benhumphreys · Mar 11, 2014 at 03:05 AM

Do you understand Mathf.Lerp? If something is not working, read the documentation.

You need to change the third parameter between 0 and 1 to change the value returned by Lerp.

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 TheWhaleDev · Mar 11, 2014 at 02:22 PM 0
Share

im trying to figure out a way for my object to slowly return to another objects rotation...it doesnt matter if im using lerp or not....i just need help figuring this rotation problem out.

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

21 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

Related Questions

Quaternion Rotation On Specific Axis Issue 0 Answers

Uniform rotation - uniform lerp 1 Answer

Rotate smoothly an object when key is up 0 Answers

Slowly Rotate to Look at 1 Answer

Flip over an object (smooth transition) 3 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