- Home /
Lerping Camera Rotation in Local Space
Hello everyone, I've been trying to setup a 1st person camera controller that allows players to (among other things) rotate the camera between fixed positions (and lerp to that position and lerp back from that position).
The problem I'm having is that I have been unsuccessful in getting the camera's rotation to be in local space, so say I want a button to rotate the camera left 30 degrees, instead of 30 degrees from center in local space, it always goes to 30 in world space and creates obvious problems.
I've tried a few solutions, but now I think I'm just making myself more confused. Any ideas are appreciated.
Answer by MrVerdoux · Jan 18, 2014 at 12:44 PM
I use RotateAround
for such things.
http://docs.unity3d.com/Documentation/ScriptReference/Transform.RotateAround.html
transform.RotateAround(transform.position, transform.up, someAngle);
After reading your comments I´m getting the feeling that I did understand your question, so I´ll try to explain this method further.
Let´s say your camera is at (100,232,-10), which are random numbers I just made up. Generally you don´t want to rotate the camera around (0,0,0), but around its own position, which is (100,232,-10). Now, that position is (0,0,0) in its own coordinates system, how would you do it? With rotate around. In a smooth way, you could create a script like this:
public class SmoothCamera : MonoBehaviour {
public float targetAngle = 0;
public float currentAngle = 0;
public float minimumDifference = 0.0001f;
public float speed = 10f;
void FixedUpdate () {
if (Mathf.Abs(targetAngle-currentAngle) > minimumDifference)
RotateLocaly(Mathf.Sign(targetAngle-currentAngle)*speed*Time.fixedDeltaTime);
}
void RotateLocaly(float angle){
transform.RotateAround(transform.position, new Vector3(0,1,0), angle);
currentAngle += angle;
}
}
Simply add it to a camera and it will look to the angle that you set at targetAngle
. And it rotates locally and smothly.
I've tried transform.Rotate, but then I ended up with trouble trying to clamp the resulting rotation.
Therefore try with RotateAround
, so you can specify where are the axis of rotation located.
Yeah, but then you still have the problem of clamping the angle with the additional problem of clamping position. I guess I just don't see why RotateAround would be useful in this case.
--It occurs to me you might have taken lerping as a desire to move the camera's position and its rotation. I'm just looking for rotation (the equivalent of turning your head left/right/up/down).
Answer by Map-Builder · Jan 18, 2014 at 03:09 PM
Qa = rotation of the Relative object you want a Qb rotation according to the Qa
Qfinal = Qa * Qb; // so your object of rotation Qf has been rotated by Qa and the Qb.
if you do not know Qb :
Qb = Qf * Quaternion.Inverse(Qa); // this is the formula to get the difference between rotation A and F.
Listen, I know this isnt an answer, but the method you are looking to invoke is Quaternion.Slerp. You would be specifying the transform.localRotation as its start point for example and then another transform`s localRotation as it`s end point, then an originial identity like transform.position or Quaternion.identity to ensure it is the base rotational direction of the transform your are moving.
Check out these references to see what i mean and find out more about quaternion rotations.
[SLERP with Quaternions][1] [LERP with quaternions][2] [This link is for three LookRotation methods available and actually are designed to deal with your issue][3]
Hope that helps in some way. Take care & thanks for reading Gruffy [1]: http://docs.unity3d.com/Documentation/ScriptReference/Quaternion.Slerp.html [2]: http://docs.unity3d.com/Documentation/ScriptReference/Quaternion.Lerp.html [3]: http://docs.unity3d.com/Documentation/ScriptReference/30_search.html?q=Quaternion.LookRotation
Thank you $$anonymous$$ap Builder and Gruffy for your answers. I tried your suggestions Gruffy and I always end up with 1) the position changing instantly, regardless to time, and 2) I have to clamp the rotation and I always end up with conversion errors.
I think I'm just don't know enough to get this done, and need to go look for some primers on Quaternions, if you guys have seen any good resources that would be great.
Think I might finally have something I'm not completely embarrassed to show: (so this just goes from a center position, to the "left" position, then back to center again). Am I on the right track?
using UnityEngine;
using System.Collections;
public class Camera$$anonymous$$ovement : $$anonymous$$onoBehaviour {
public Transform defaultRotation;
public Transform leftRotation;
void Update ()
{
$$anonymous$$oveCamera ();
}
void $$anonymous$$oveCamera ()
{
if (Input.Get$$anonymous$$ey ($$anonymous$$eyCode.Y))
{
Vector3 relativePos = (leftRotation.position - transform.position);
Quaternion rotation = Quaternion.LookRotation (relativePos);
Quaternion current = transform.localRotation;
transform.localRotation = Quaternion.Slerp (current,rotation, Time.deltaTime * 10.0f);
}
else
{
Vector3 relativePos = (defaultRotation.position - transform.position);
Quaternion rotation = Quaternion.LookRotation (relativePos);
Quaternion current = transform.localRotation;
transform.localRotation = Quaternion.Slerp (current,rotation, Time.deltaTime * 10.0f);
}
}
}
Your answer

Follow this Question
Related Questions
Multiple Cars not working 1 Answer
A node in a childnode? 1 Answer
Distribute terrain in zones 3 Answers
More ways to use Trail Renderer 1 Answer
Restricition of Objects. 1 Answer