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]
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.
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?
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.
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.
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.
After spending 2 days of searching your answer helps me a lot.. $$anonymous$$illions of thanks :)
I love this! Lookout for your offset. It needs to be dynamic offset in order to maintain distance and direction. Good work homie.
Works like a charm! Thanks a lot @LordScottWarren . This must be the accepted answer