• 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 midomido · Oct 08, 2011 at 11:10 AM · rotationmovementsmooth

smooth rotation / movement ?

i am making a 2D game that has 3d rotation with camera i can make it rotate and change axis but i want to make it smoothly transition how can i make smooth rotation ? and smooth movement ? this is how i do it :- transform.Rotate(0,-90,0); i want it to be smooth and in movement there is Distance from player i change the distance from X 10 to X = 0 and Z 0 to Z = 10 but all happen instantly how can i fix 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

2 Replies

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

Answer by aldonaletto · Oct 08, 2011 at 11:28 AM

Smooth in Unity means Lerp! (Slerp, for Quaternions) You should use the "Lerp filter", a smart use of Lerp that behaves like a smoothing filter. Attach this script to the camera:

var speed: float = 2.5; var cameraAngles: Vector3; // the target angles around x, y and z var cameraPos: Vector3; // the target position

function Start(){ cameraAngles = transform.eulerAngles; cameraPos = transform.position; }

function Update(){

 transform.position = Vector3.Lerp(transform.position, cameraPos, speed*Time.deltaTime);
 var newRot = Quaternion.Euler(cameraAngles); // get the equivalent quaternion
 transform.rotation = Quaternion.Slerp(transform.rotation, newRot, speed*Time.deltaTime);

} You just set the angle and position you want in cameraAngles and cameraPos, and the camera goes smoothly to the new rotation/position.

Comment
Add comment · Show 4 · 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 midomido · Oct 08, 2011 at 01:25 PM 0
Share

thank you.

avatar image Kinger6621 · Jan 07, 2016 at 12:08 AM 0
Share

so cameraPos is the target position?

avatar image Spyter · Feb 16, 2016 at 06:28 PM 0
Share

I know this is an old post; however, I just came across it when I was searching for lerping examples and I wanted to comment on it because I think it may be incorrect. I am new to unity but after reading another site, I don't think you can just call Vector3.Lerp() like you are doing in the example below:

 transform.position = Vector3.Lerp(transform.position, cameraPos, speed*Time.deltaTime);
  var newRot = Quaternion.Euler(cameraAngles); // get the equivalent quaternion
  transform.rotation = Quaternion.Slerp(transform.rotation, newRot, speed*Time.deltaTime);

I believe you need to store the original position of the object and the end position when you begin lerping so you can use it in each FixedUpdate() call. Every time FixedUpdate() is called, transform.position will be different during an interpolation and unless I am missing something, speed*Time.deltaTime is incorrect too. Vector3.Lerp() only interpolates the value given in the third parameter between two points. It is almost like a "percentage" of how far the lerp is supposed to move between the start/end points. Lerping will span across several FixedUpdate() calls. You can't just magically call Vector3.Lerp(), give it the three parameters and forget about it.

Lerping should begin in Update() and be handled in FixedUpdate() to ensure consistency across a variable frame rate.

Same lerping concepts apply for both movement and rotations.

See below for a better example/explanation of what I mean:

http://www.blueraja.com/blog/404/how-to-use-unity-3ds-linear-interpolation-vector3-lerp-correctly

avatar image meat5000 ♦ Spyter · Feb 16, 2016 at 06:36 PM 0
Share

@Spyter

$$anonymous$$ate, check out this guy's $$anonymous$$arma (Reputation). I think he has a good idea ;)

You can magically call Lerp and forget about it if you have placed it in Update, which happens to be a Loop. And the nature of Variables mean that the information is Updated frame on frame.

There is an unusual but very useful application of Lerp referred to as the Easing Lerp that moves the object along by a FIXED percentage (hence the speed*deltaTime) between itself and some point. The effect is that you will achieve smooth movement between those two ever updating points and the Lerp never runs out.

Super cool, eh?

avatar image
0

Answer by torsten · Oct 08, 2011 at 12:13 PM

Hi,

I don't exactly understand what you mean, but have a look at Quaternion.Slerp() for smooth interpolation of rotation and for translation Vector3.Lerp().

Torsten

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

7 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Smooth rotation... 1 Answer

Smooth Attached Object movement 1 Answer

Undesirable character flipping 0 Answers

Can't force my circle sprite to move/rotate smoothly. Code is very simple. 0 Answers

Character Controller slides sideways when it hits objects are angles different from 90 degrees 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