I notice you have another thread about counter acting gravity.
If you are attempting to use this to counteract gravity, you would merely impulse in the opposite direction of gravity. Of course, because gravity is accelerative, and not constant, we need to take into account the acceleration that will occur that frame:
using UnityEngine;
using System.Collections;
public class TestScript03 : MonoBehaviour {
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if(Input.GetMouseButtonDown(0))
{
this.rigidbody.AddForce(Vector3.left * 10f);
}
if(Input.GetMouseButtonDown(1))
{
this.rigidbody.AddForce(Vector3.right * 10f);
}
}
void FixedUpdate()
{
var gdir = Physics.gravity.normalized;
var d = Vector3.Dot(this.rigidbody.velocity, gdir);
d = Mathf.Max(0f, d); //if we don't clamp this we'd also remove upward velocities, if you want to do that as well... comment out this line
d += Physics.gravity.magnitude * Time.deltaTime;
this.rigidbody.AddForce(-d * gdir * this.rigidbody.mass, ForceMode.Impulse);
}
}
Here I show a script that you can attach to a rigidbody with gravity applied to it. Set its mass to something to demonstrate it works despite mass.
Note it’ll float in space, but if you click your mouse it’ll start to move left or right (depending the mouse button).
Now that’s sort of what ForceMode.Accelation is for, so you could not use Impulse, and instead just use ForceMode.Acceleration:
void FixedUpdate()
{
//var gdir = Physics.gravity.normalized;
//var d = Vector3.Dot(this.rigidbody.velocity, gdir);
//d = Mathf.Max(0f, d);
//d += Physics.gravity.magnitude * Time.deltaTime;
//this.rigidbody.AddForce(-d * gdir * this.rigidbody.mass, ForceMode.Impulse);
this.rigidbody.AddForce(-Physics.gravity, ForceMode.Acceleration); //does the same thing if starting from rest
}
BUT, this implies that we started from rest.
If you start this after the object is in motion, all it’ll do is stop the acceleration of gravity, making the downward velocity constant rather than 0.
Where as impulse will counteract no matter what.