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 betaFlux · Sep 09, 2014 at 06:42 PM · lerpslerpconstant

Constant Transform Lerp

Is it bad practice to let a transform constantly re-position and rotate itself to a target with Vector3.Lerp and Quaternion.Lerp? For example:

 Camera.main.transform.position = Vector3.Lerp(Camera.main.transform.position, head.position, cameraSlerpPositionSpeed * Time.deltaTime);
 Camera.main.transform.rotation = Quaternion.Lerp(Camera.main.transform.rotation, head.rotation, cameraSlerpRotationSpeed * Time.deltaTime);

Is this valid or is it something a programmer should avoid?

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

3 Replies

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

Answer by rutter · Sep 09, 2014 at 06:58 PM

In many cases, all a game does is reposition and rotate things. :) Go ahead and do so as much as you like.

However, many people misunderstand what lerp does. Given two values, start and end, a lerp returns another value that is X% between them.

In your example code, if cameraSlerpPositionSpeed is 1 and your game runs at 50 FPS, you'll generally move the camera 2% closer to its target each frame. If cameraSlerpPositionSpeed is 40, you'll move it 80% closer each frame.

Now, you can call lerps with a fixed value to create a sort of "easing" effect. If your object moves 50% closer each frame, it will start out moving very quickly, and will slow down as it nears its target... but it won't necessarily ever reach that target.

Your variable naming and function params give me a feeling that you're not trying to do that, though.

If you expect a rate like "camera moves X meters per frame" or "camera rotates Y degrees per frame", you either need to set up your lerp calls with a timer, or use helper functions like Vector3.MoveTowards and Quaternion.RotateTowards:

 transform.position = Vector3.MoveTowards(transform.position, goalPosition, metersPerSecond * Time.deltaTime);
 transform.rotation = Quaternion.RotateTowards(transform.rotation, goalRotation, degreesPerSecond * Time.deltaTime);
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 betaFlux · Sep 09, 2014 at 07:30 PM 0
Share

Thanks alot rutter, what I'm trying to do is to switch between first person views of multiple characters on mouse click. I tried a more complicated way with lerp and parenting the camera to the head bone to prevent a performance hit, but now after reading the answers to my question, I am glad a single lerping camera will be enough.

avatar image
2

Answer by KMKxJOEY1 · Sep 09, 2014 at 06:57 PM

perfectly valid; it does use some computational expensive methods but unless there is a whole lot of objects using these methods then you should be good :) Always a plus when programmers are thinking about performance though!

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 betaFlux · Sep 09, 2014 at 07:43 PM 0
Share

Thanks for the confirmation! Luckily I just need to move the main camera around the scene that way.

avatar image
1

Answer by ThePersister · Sep 09, 2014 at 07:00 PM

That's perfectly fine for as far as I know, just be careful with using .transform in Updates. You probably know GetComponent() is heavier on the system than things with variables, right?

Well, .transform is also actually a GetComponent() in disguise. So you should consider storing the transform in a variable and then using that. Which ends up in:

 Transform mCameraMainTrans;
 
 void Start() {
     mCameraMainTrans = Camera.main.transform;
 }
 
 void Update() {
     mCameraMainTrans.position = Vector3.Lerp(mCameraMainTrans.position, head.position, cameraSlerpPositionSpeed * Time.deltaTime);
     mCameraMainTrans.rotation = Quaternion.Lerp(mCameraMainTrans.rotation, head.rotation, cameraSlerpRotationSpeed * Time.deltaTime);
 }

So yea, for as far as I know it's pretty valid. However I'd be englightened if told otherwise by a more experienced Unity Programmer, so I'm only 90% sure :)

Also, I've surely seen algorithmic structures that go far far far beyond this and still work out perfectly fine, so you should be fine either way ;)

If you like my answer enough, you can accept it, I'd appreciate it :)

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 betaFlux · Sep 09, 2014 at 07:48 PM 0
Share

Thanks for the advice, I sometimes find it kind of enoying to do it for every component in the script, but you are right. The advantage is obvious. :)

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

26 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

Related Questions

How to use Slerp / Lerp / RotateTowards to rotate an object (from-to) two rotations but around their Y axis only? 0 Answers

Slerp / lerp not creating a smooth transition 2 Answers

Is it possible to use a quaternion from a gameobjects position to another gameobjects position? 1 Answer

(s)lerp y-rotation to the direction of velocity 0 Answers

Rotation with RotateAround 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