- Home /
How Can I Linear Interpolate (Lerp) in Local Space?
I'm trying to smooth the motion of my camera by using Lerp (I had previously been using Transform.translate). This has worked for panning my camera left and right, but I have my camera pitched downward at 45 degrees and I would like my scroll wheel to move the camera in Local Z (ie, along the camera view path).
Lerp cannot take Space.Self as an argument, so how can I achieve this?
(If possible a C# solution would be appreciated, but I can translate a JS answer to C#)
Answer by srsobieraj · Feb 21, 2012 at 01:14 AM
Solved.
You can get the local vector by using transform.InverseTransformDirection().
Vector3 relativeMoveVector = transform.InverseTransformDirection(moveVector);
myCamera.position = Vector3.Lerp(myCamera.position, myCamera.position + relativeMoveVector, Time.deltaTime * smoothingFactor);
Answer by Berenger · Feb 20, 2012 at 04:01 AM
Keep in mind that lerp is a really straight forward function. It's "how much i do take from a, and how much do I take from b". Thus, the space you're working on is irrelevant. What you mean is a lerp between a position and that position minus a certain distance along it's forward vector (here is the space.self). It's up to you to find that a and that b. Then the wheel will control the t in Lerp(a, b, t).
It's up to you to find that a and that b.
I understand that. I'm having a hell of a time getting my value for 'b' though. $$anonymous$$y 'a' value is just the .position of my camera, and t is negotiable depending on how much I want to smooth everything.
However, 'b' is escaping me. Here's my snippet:
Vector3 moveVector = Vector3.forward * Input.GetAxis("$$anonymous$$ouse ScrollWheel") * zoomSpeed * Time.deltaTime;
if (IsInBounds(moveVector))
{
myCamera.position = Vector3.Lerp(myCamera.position, myCamera.position + moveVector, Time.deltaTime * smoothingFactor);
}
The way you're calculating mouseVector, you don't need Lerp : just Tranlaste in world space or transform.position += mouseVector. However, you wont have any boundaries, which mean you can step back infinitely or go further than your target. It's not a bad solution, just be aware of it. If you're having a free camera, it's actually not a problem.
How would I calculate moveVector in a way that I can have boundaries?
I'd like my camera to have smoothed motion and be bounded.
Thanks in advance, I realise I'm asking a lot of questions.
I had in $$anonymous$$d a camera focus on a target, then I would have a startPos near the target, an endPos away from the target, then moved the camera like that transform.position = Vector3.Lerp( startPos, endPos, wheel ) with wheel the mouseWheel between 0 and 1.
But if you have a pan and maybe a lookaround, that's more complicated. you'll have to know if your inside the sphere / box that represent your world I guess.