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);
}
}
}
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?
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.
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.