- Home /
How would I Lerp or interpolate this?
Is there a way I can Lerp or interpolate this so that the object doesn't just "transport" to the new location but rather moves smoothly? I can usually fumble my way through JS but I am new to C# and everything I try throws an error. Here is my current code:
void SetTransformY(float n){
transform.position = new Vector3(transform.position.x, n, transform.position.z);
}
// Update is called once per frame
void Update () {
if(NetworkManager.rocketButtonState == 0){
SetTransformY(1.0f);
}
else if(NetworkManager.rocketButtonState == 1){
SetTransformY(0.2f);
}
else if(NetworkManager.rocketButtonState >= 2){
SetTransformY(0.1f);
}
}
thank you
Answer by Bonfire-Boy · Feb 07, 2015 at 04:43 AM
Try something like this for starters...
Update()
{
SetTransformY(transform.position.y + speed * Time.deltaTime);
}
Where speed is a float you can try different values for. Multiplying it by deltaTime makes the distance moved each frame depend on how long it is since the previous frame, so the actual speed is constant. You could make your button presses alter the value of speed.
Looks like this code would just move my object up in the air at a constant speed. edit: tried it anyway and receive an error "error CS1520: Class, struct, or interface method must have a return type"
$$anonymous$$ake sure that you include a return type of 'void' with Update()
@BadSeedProductions Yeah sorry that wasn't complete code, just showing you a general method for smooth movement, using your own SetTransformY() function. If you want a different direction you can change what elements of the vector you're changing, or just use Vector3.Translate(). If you want it to stop when it gets to a particular point you'd need to add that too. Or you can use $$anonymous$$oveTowards() which handles both those aspects.
Your answer
Follow this Question
Related Questions
NetworkTransform not interpolating? 0 Answers
Smooth Camera transition (linear interpolation) Error 1 Answer
can someone explain how using Time.deltaTime as 't' in a lerp actually works? 5 Answers
Take my useful script - but help me improve it? 2 Answers
Mathf.Lerp is just jumping to maximum, no 5.0 interpolation. 1 Answer