help lerping gravity

i am trying to lerp the gravity in game, i can change the ravity direct in code but havingtrouble using mathf lerp, can anyone show me where i am going wrong

thank you

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class GRAVMANAGERACCESS2 : MonoBehaviour {


        public float duration;
        public float myvar;
private float t = 0;

    // Use this for initialization
    void Start () {
    //    Physics.gravity = new Vector3(0f, 5f, 0f);
t = 0;
       
       
    }
   
    // Update is called once per frame
    void Update () {
        if (t < 1){
     t += Time.deltaTime/duration;

    //    Physics.gravity.y = new Vector3(Mathf.Lerp(-9.81f, 9.81f, t));

        myvar = Vector3(Mathf.Lerp(-9.81f, 6.0f, t));

    //    Physics.gravity.y = Mathf.Lerp(-9.81f, 9.81f,t);

        Physics.gravity = new Vector3(0, myvar, 0);


       
    }
}
}

I assume you want the gravity to go from -9.81 to +6.0 over the course of duration from the start of this Monobehavior’s life?

void Update()
{
  t += Time.deltaTime;

  float fraction = t / duration;

  grav = Mathf.Lerp( -9.81f, 6.0f, fraction);

  Physics.gravity = new Vector3( 0, grav, 0);
}

Just make sure duration is nonzero!

1 Like

thank you, seems like i nearly had it right, very grateful for your insight, will implement tomorow thanks again

1 Like

Actually the only line in the original code that need to be fixed is this one:

The “Vector3” part doesn’t make much sense as myvar is a float and Lerp returns a float. So when doing

myvar = Mathf.Lerp(-9.81f, 6.0f, t);

It should compile and does the same as Kurt’s solution.

after testing i see the transition is pretty harsh so i tried to change the lerp for smoothstep but it seemed the same, is there a way to smooth the in and out?

Well if you want to have an ease in and ease out you can simply use a hermit curve to “distort” your t value. I’ve posted an example over here.

public static float Hermit(float t)
{
    return 3*t*t - 2*t*t*t;
}

// [ ... ]
Mathf.Lerp(-9.81f, 6.0f, Hermit(t));

Though note that you should clamp the incomming t between 0 and 1 as the curve only behaves as we want in that interval

You can clamp the t value by using Mathf.Clamp01(t) so you may do:

Mathf.Lerp(-9.81f, 6.0f, Hermit(Mathf.Clamp01(t)));

or clamp it inside the Hermit function

public static float Hermit(float t)
{
    t = Mathf.Clamp01(t);
    return 3*t*t - 2*t*t*t;
}

This presumes your gravity was already -9.81 at the start, otherwise there will be a sharp change to gravity at the start. Also, how long is your duration?

Remember, gravity is the second derivative of position, ie., gravity is acceleration, NOT speed. The gravity value affects the change over time of the speed (somewhat observable), and the speed affects the change over time of the position (highly observable), so you are two orders removed from controlling position, which is the primary observable result. Direct changes to gravity, especially gradual ones, are not very immediately observable unless an object is initially at rest.

Do you intend to control the velocity instead? You can use the same code above to do that, just drive the velocity parameter on your rigidbody, or one of the axes of it.

1 Like

i probably meant instead of harsh that i wanted to start slow and speed up then gradually slow down again at the end of the lerp, i will have a go at the examples above see if i get it to work

Gravity is an acceleration. Over time it will affect a speed. If you want the speed to slow down, then the gravity must be opposite direction of the speed.

Again if you’re looking for a speed behavior, either graph it, then take the first derivative of it, and that will be what the gravity must be to accomplish it. Otherwise you are controlling the wrong value.