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
5
Question by Joe-the-programmer · Dec 25, 2014 at 06:55 PM · transform.lookat

How to slow down transform.LookAt

Im trying to make the camera move with the mouse pointer in my game using:

 public class camera : MonoBehaviour {
     public static Vector3 Point;
     public float zDistance;
 
     void FixedUpdate(){
         var mousePos = Input.mousePosition;
         Point=camera.ScreenToWorldPoint(new Vector3(mousePos.x,mousePos.y,zDistance));
         transform.LookAt(Point);
     }
 }v

It works but the screen moves really fast, too fast to be able to play properly. Is there any way to slow it down?

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 Wisearn · Dec 25, 2014 at 07:19 PM 2
Share

You can save the current rotation as "from", and then use transform.lookat and then save the rotation as "to" and then set the rotation to quaternion.lerp from, to, Time.deltaTime as time and in update ins$$anonymous$$d of fixedupdate

And then you can multiply time if you want it faster or slower like Time.deltaTime*0.5f

6 Replies

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

Answer by EvilTak · Dec 26, 2014 at 05:35 AM

From my answer to this question: you should use Quaternion.FromToRotation and then use Quaternion.Lerp. Something like this (This code should be in FixedUpdate for smooth results):

  Vector3 direction = Point - transform.position;
 Quaternion toRotation = Quaternion.FromToRotation(transform.forward, direction);
 transform.rotation = Quaternion.Lerp(transform.rotation, toRotation, speed * Time.time);

You can set the speed variable to how slow or fast you want the rotation to be. You can look at the result here (although there the y axis of the object is pointing towards a point).

Comment
Add comment · Show 2 · 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 qskakar · Jun 20, 2018 at 07:00 AM 0
Share

Checkout @Cheeseman 's answer if this doesn't work.

avatar image WILEz1975 · Oct 12, 2021 at 07:08 PM 0
Share

Why Time.time? It could be Time.deltaTime?

avatar image
11

Answer by mxoconnell · Sep 02, 2019 at 01:19 AM

         Vector3 relativePos = focus.position - transform.position;
         Quaternion toRotation = Quaternion.LookRotation(relativePos);
         transform.rotation = Quaternion.Lerp( transform.rotation, toRotation, 1 * Time.deltaTime );
Comment
Add comment · Show 3 · 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 mpbMKE · Feb 05, 2020 at 06:43 PM 0
Share

So Lerp and Slep work fine to get good ease-out animation on rotation, but for good naturalistic movement you need ease-in, as well. $$anonymous$$athf.SmoothStep can help on anything that accepts float values, but Quaternions are trickier to implement that way.

I'm kind of fishing here, but do you know of a good way to ease rotation on both sides of the animation?

avatar image mrVentures mpbMKE · Oct 14, 2021 at 05:59 PM 0
Share

I do not, sorry. (same person)

avatar image Pangamini mrVentures · Oct 14, 2021 at 06:14 PM 1
Share

This kind of misuse of lerp with deltaTime is like a disease. People keep spreading it, newcomers keep catching it.

avatar image
5

Answer by Cheeseman · Jun 03, 2018 at 09:11 PM

I know this post is super old, but I wanted to amend @Evil-Tak 's post:

If Quaternion.FromToRotation() doesn't work, try using Quaternion.LookRotation() in it's place (slightly different params).

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 qskakar · Jun 20, 2018 at 06:59 AM 0
Share

Thank you. I was wondering why @Evil-Tak 's method wasn't working. You solved my problem.

avatar image
1

Answer by Stormakt · Feb 25, 2019 at 01:48 AM

Adding on, when I tried the code above from @EvilTak, it instantly turned. I changed Time.timeto Time.deltaTime to resolve this.

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
1

Answer by CodeMonkeyYT · May 03 at 09:12 AM

Everyone else is already correctly mentioning how to modify transform.rotation, but personally I find Quaternions to be really confusing so my preferred method for rotating is by directly modifying transform.forward

You just calculate the direction vector with (targetPosition - transform.position).normalized

You can combine it with Vector3.Lerp to get a nice smooth rotation https://unitycodemonkey.com/video.php?v=jAN2IoWdPzM

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
  • 1
  • 2
  • ›

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

14 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

Related Questions

How to catch a rotation from transform.LookAt function? 0 Answers

transform.LookAt(target); 4 Answers

Need help with Transform.LookAt 1 Answer

Creating a homing missile on 2D, problem with transform.lookat() 1 Answer

How to do transform.LookAt() from a different part of object? 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