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 /
  • Help Room /
avatar image
1
Question by justarandomname2 · Jan 27, 2017 at 11:28 PM · unity 5camerarotationlerplookat

Smooth Look at...

Gosh I've been searching for that like hours and there are tons of questions already out there I copy and pasted all of them I went through them searched for errors but I just couldnt manage to have an object smoothly rotate towards my first persons character. So what I then did was:

person.transform.rotation = Quaternion.Lerp(person.transform.rotation, kamera.transform.rotation, 0.1f);

Now that at least did something, but it turned the object that should look at me in the completely wrong direction and on all axis, I only wanted to move on the Y-axis. So I did this

person.transform.rotation = Quaternion.Lerp(person.transform.rotation, new Quaternion(0, kamera.transform.rotation.y, 0, kamera.transform.rotation.w), 0.1f);

and finally the object ("person") rotated only on the y axis and completely parallel to my Rotation, but still in the wrong direction! So I found out that Unity's Rotation does not go from 0 to 360 but rather from -1 to 1 and so to Switch the Rotation all i had to do was multiplying with -1, I thought. So I changed "kamera.transform.rotation.y" to "kamera.transform.rotation.y [times]- 1" but guess what happened? That shitty object still rotates in the complete opposite and yes I saved the script I also did Debug.Log to see if it executes that code and it did!! >.< Also now the object at some points does not rotate at all anymore or in some weird direction (after adding " [Times] - 1"). How can I do this? And why is Unity so damn complicated with the easy things?

By the way: If you see "MINUS 1" I mean "TIMES MINUS 1" but somehow when i write * - 1 it Automatically turns into -1 Edit: Ok now it writes it in that sentence ↑ Interesting... Ededit: I think it has to do with the Feature that stars make text bold I changed them to [Times]

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
7
Best Answer

Answer by HenryStrattonFW · Jan 28, 2017 at 11:50 AM

The rotations are not mapped from -1 to 1 in the same sense as a 0-360 range. the reason they are represented as four numbers in that range is because they are represented as Quaternions, A good rule of thumb is to never mess directly with the x,y,z,w values of a quaternion unless you know what you're doing (or are a wizard, those guys can do what they want). If you want to observe/modify the angles of a transforms rotation in a more user friendly way, try looking at the transform.rotation.eulerAngles, which will be in degrees.

But in regards to the problem in general you are having. here is a quick script that will get the object it is placed on to smoothly turn to face the target object, but will only rotate around the Y axis.

 using UnityEngine;
 
 public class TestLookAt : MonoBehaviour
 {
     [SerializeField]
     private Transform m_Target;
     [SerializeField]
     private float m_Speed;
 
 
     void Update()
     {
         Vector3 lTargetDir = m_Target.position - transform.position;
         lTargetDir.y = 0.0f;
         transform.rotation = Quaternion.RotateTowards(transform.rotation, Quaternion.LookRotation(lTargetDir), Time.time * m_Speed);
     }
 }
 

Also keep in mind that these rotations are based on the idea that your objects "facing direction" is along the positive Z axis, If your object is not set up like this then it will likely always face in the wrong direction but consistently in its offset.

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 justarandomname2 · Jan 28, 2017 at 05:53 PM 0
Share

Hi, thanks! One thing though, how can I make the person randomly rotate smoothly? So I have a Boolean that is randomly set to true and randomly to false, which works fine. But now I want to rotate the person with your method smoothly in a random y rotation. So in Update i would say:

 if (myBool == true)
 {
     int rotation = Random.Range(0, 359);
     Vector3 lTargetDir = m_Target.position - transform.position;
     lTargetDir.y = 0.0f;
     transform.rotation = Quaternion.RotateTowards(transform.rotation,
            Quaternion.LookRotation(lTargetDir), Time.time * m_Speed);
 }

like this he again rotates to the camera (target) ignoring the variable 'rotation'. How can I do it that the target is not my camera but rather it's not looking for a Vector3 but for a Quaternion so I can set it's Y rotation to the 'rotation' variable.

Pseudocode:

 int rotation = Random.Range(0, 359)
 Quaternion newRotation = transform.rotation;
 newRotation.Y = rotation;
 transform.rotation = Quaternion.RotateTowards(transform.rotation,
 newRotation, Time.time * m_Speed);

That code does not work though

PS: How do you make that code (highlighting and line numbers)? Is this a screenshot?

avatar image HenryStrattonFW justarandomname2 · Jan 28, 2017 at 07:12 PM 0
Share

You forgot my advice, never change the x,y,z,w components of a Quaternion unless you know what you're doing (or are a unicorn, those things get a free pass). use rotation.eulerAngles when you're trying to deal with [0-360] space.

As to your issue, not 100% sure on what you are trying to do, but If I understand correctly, you want your object to smoothly turn to face a target direction, but have that target direction randomly change? If so, give this script a try.

 using UnityEngine;
 
 public class TestLookAt : $$anonymous$$onoBehaviour
 {
     [SerializeField]
     private float  m_ChangeInterval;
     [SerializeField]
     private float m_Speed;
 
     private Vector3 m_TargetDirection;
 
     void Awake()
     {
         InvokeRepeating("_GetNewTarget", 0, m_ChangeInterval);
     }
 
     void Update()
     {
         transform.rotation = Quaternion.RotateTowards(transform.rotation, Quaternion.LookRotation(m_TargetDirection), Time.deltaTime * m_Speed);
     }
 
     private void _GetNewTarget()
     {
         m_TargetDirection = Quaternion.AngleAxis(Random.Range(0, 360), Vector3.up)* Vector3.forward;
     }
 }

It's the same rotation code as before, except I swapped to deltaTime ins$$anonymous$$d of time (using time was a mistake on my part, this will work better, just have to give it a higher speed value). It also randomly generates a new target direction ins$$anonymous$$d of calculating that direction from a transform target.

As for posting code, when posting an answer, comment, or question there is a little button in the toolbar that has next to the paper clip with binary on it, that brings up a window where you can post code that will be better formatted and easier for people to read.

avatar image Super_Solomob422 · Jun 25, 2020 at 12:21 PM 1
Share

Well. Time.time is the length of time since the application has been running. So I don't think that using that there is a good idea, something more like Time.deltaTime is a better idea. I'm not sure but maybe multiplying the speed by Time.deltaTime and not the other way around.

 {
      [SerializeField] private Transform target;
      [Range(1,10)] [SerializeField] private float speed = 1f;
  
 
          void Update()
          {
              Vector3 lTargetDir = target.position - transform.position;
              lTargetDir.y = 0.0f;
              transform.rotation = Quaternion.RotateTowards(transform.rotation,
  Quaternion.LookRotation(lTargetDir), (speed * Time.deltaTime) *  50);
          }
      }

So, something like this, I multiplied it by 50 again so it goes faster because it tended to be slow, I find 5 to be a realistic speed for a human head with this. Also multiplying by Time.deltaTime makes sure it goes the same speed regardless of framerate.

avatar image
13

Answer by LordScottWarren · Jul 20, 2018 at 01:12 PM

I actually found a better option that worked for me, because the Update only gets called once per frame you can take to rotation of the LookAt and use that to lerp with.

 Quaternion OriginalRot = transform.rotation;
 transform.LookAt(Target);
 Quaternion NewRot = transform.rotation;
 transform.rotation = OriginalRot;
 transform.rotation = Quaternion.Lerp(transform.rotation, NewRot, speed * Time.deltaTime);

This is a script attached to the camera therefore the transform is the cameras. Hope this helps people.

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 Tekko · Feb 01, 2019 at 03:48 PM 0
Share

$$anonymous$$inda janky setup but it works fine for me :D

avatar image samra2494 · May 13, 2020 at 06:26 AM 0
Share

After spending 2 days of searching your answer helps me a lot.. $$anonymous$$illions of thanks :)

avatar image aloften · May 16, 2020 at 07:29 PM 0
Share

I love this! Lookout for your offset. It needs to be dynamic offset in order to maintain distance and direction. Good work homie.

avatar image srDeivid · Apr 08, 2021 at 11:36 PM 0
Share

Works like a charm! Thanks a lot @LordScottWarren . This must be the accepted answer

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

11 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

Related Questions

Using a transform.Lookat to set a rotation for a Vector3.lerp. 2 Answers

Creating a Window - Camera movement 0 Answers

Why isn't my object lerping ? C# 2 Answers

Camera rotate X degree based on player rotate 0 Answers

my camera is rotating on Z axis when i just set it on X and Y 0 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