- Home /
Lerp transition/rotation?
Hello, I'm trying to make an object use lerp to transition from one transform/rotation to another. My code doesn't work the way I want it to. It's really jerky, and it transitions instantly instead of steadily.
var start : Transform;
var end : Transform;
function Update () {
if( Input.GetButtonDown("Fire2")) {
transform.position = Vector3.Lerp(start.position, end.position, Time.time);
}else{
transform.position = Vector3.Lerp(end.position, start.position, Time.time);
}
}
Can someone correct it, please?
Answer by testure · Jun 13, 2011 at 07:36 AM
start and end aren't changing- so it can't lerp. you're telling it to do the exact same thing every frame- the jerkiness is probably a side effect of that.
try something like this (untested C# off the top of my head, sorry)
private Vector3 targetPosition = Vector3.zero;
private float [] dampVelocity = {0,0,0};
private float transitionSmoothing = 10f;
void Update () {
if( Input.GetButtonDown("Fire2"))
{
targetPosition = new Vector3(10f, 0f, 10f);
}
else
{
targetPosition = new Vector3(-10f, 0f, -10f);
}
for(int i = 0; i < 3; i++)
{
transform.position[i] = Mathf.SmoothDamp(transform.position[i], targetPosition[i], ref dampVelocity[i], transitionSmoothing * Time.deltaTime);
}
}
Assets/Scripts/Camera/Aim_position.js(1,9): BCE0043: Unexpected token: Vector3.
Assets/Scripts/Camera/Aim_position.js(2,9): BCE0043: Unexpected token: float.
Assets/Scripts/Camera/Aim_position.js(2,17): UCE0001: ';' expected. Insert a semicolon at the end.
Assets/Scripts/Camera/Aim_position.js(2,35): BCE0044: expecting :, found ','.
I tried inserting some colons into the beginning, but it didn't work.
yeah- you're not going to be able to just plug it into your code, you'll have to read the code I wrote, understand what it's doing, and then interpret it. either that, or put it in a CS file and hope 'off the top of my head' is good enough for a compiler :)
It's better if you don't post code in a different language than the question uses. Quite a few people only know one language.
Answer's aren't just for the person who asked them.
Personally, if I were searching for something, I'd much rather run across an answer that was posted in a slightly different language than nothing at all.
Of course, this implies that the OP searched in the first place which obviously isn't the case, but others may search and find it useful. If all people do is copy and paste psuedocode from an answer and paste it into a JS file, they're not doing themselves any favors anyway.