- Home /
m_MouseLook.LookRotation After Quaternion.LookRotation on some object
Hello friends, i need some help. My task is at a certain moment in time, forcibly, to fix the player's camera of standart player controller script on a certain object. After that, the player's camera needs to remain on the object. I wrote a script like this. But it does not work correctly. The camera is not positioned correctly after stop fixing. Pls help to find mistake.
private void Update()
{
if (NeedToSeeSmth)
{
m_Camera.transform.rotation = Quaternion.Slerp(m_Camera.transform.rotation, Quaternion.LookRotation(ObjectsForView.transform.position - m_Camera.transform.position), 5f * Time.deltaTime);
transform.rotation =Quaternion.Slerp(transform.rotation,Quaternion.LookRotation(ObjectsForView.transform.position -transform.position), 5f * Time.deltaTime);
NeedLoadRotation = true;
}
else
RotateView();
}
////////
private void RotateView()
{
if (NeedLoadRotation)
{
m_MouseLook.m_CameraTargetRot = m_Camera.transform.rotation;
m_MouseLook.m_CharacterTargetRot = transform.rotation;
m_MouseLook.Init(transform, m_Camera.transform);
NeedLoadRotation = false;
}
m_MouseLook.LookRotation(transform, m_Camera.transform);
}
When you say "positioned", do you mean rotated or actually not in the right position anymore?
Answer by VoidVenom · Sep 04, 2020 at 10:08 PM
You'll need to adapt this to fit your script, but here's a working mechanism:
public GameObject objectToLookAt;
public bool forceLookAt = false;
private bool rotationSaved = false;
private Quaternion savedRotation;
void Update() {
if (forceLookAt) {
// Save the rotation if needed
if (!rotationSaved) {
savedRotation = Camera.main.transform.rotation;
rotationSaved = true;
}
// Create a transform that looks in the direction of the target
Transform targetTransform = Camera.main.transform;
targetTransform.LookAt(objectToLookAt.transform);
Camera.main.transform.rotation = Quaternion.Slerp(Camera.main.transform.rotation, targetTransform.rotation, Time.deltaTime * 5.0f);
} else if (rotationSaved) {
Camera.main.transform.rotation = Quaternion.Slerp(Camera.main.transform.rotation, savedRotation, Time.deltaTime * 5f);
if (Camera.main.transform.rotation == savedRotation)
rotationSaved = false;
}
}
}
Your answer
Follow this Question
Related Questions
Geometry Wars game experience in Unity 2 Answers
How can I pause the "Mouse Look" script when I pause my game? 2 Answers
turn mouse look on and off using the 'm' key 2 Answers
Problem with the standard mouselook script 0 Answers
MouseY always enabled? 3 Answers