Mesh based gravity
I seek your assistance for a quite peculiar gravity. I currently work with repulsive gravity that works relatively well with small bowl mesh. But with huge irregular mesh, the player takes off when I stop runing, not properly adjusted with the ground normal and other unwanted things.
This is just a test mesh. Not the definitive.
I think to use Raycast and the ground normal to change gravity vector but I don't know how to do. I try different ways without any result... Some function in the scripting reference like this one : http://docs.unity3d.com/ScriptReference/EventSystems.RaycastResult-worldNormal.html is not recognized by unity.
I use this tutorial for the character : https://www.youtube.com/watch?v=b0PvJ4AWvWQ but not sure it adapts well to my code herewith.
FauxGravity:
public class FauxGravity : MonoBehaviour
{
public float gravity = -10;
// Use this for initialization
public void Attract (Transform body)
{
Vector3 gravityUp = -(body.position - transform.position).normalized;
Vector3 bodyUp = body.up;
body.GetComponent<Rigidbody> ().AddForce (gravityUp * gravity);
Quaternion targetRotation = Quaternion.FromToRotation (bodyUp, gravityUp) * body.rotation;
body.rotation = Quaternion.Slerp (body.rotation, targetRotation, 50 * Time.deltaTime);
}
}
FauxGravityBody:
public class FauxGravityBody : MonoBehaviour
{
public FauxGravity attractor;
private Transform myTransform;
// Use this for initialization
void Start ()
{
GetComponent<Rigidbody> ().constraints = RigidbodyConstraints.FreezeRotation;
GetComponent<Rigidbody> ().useGravity = false;
myTransform = transform;
}
// Update is called once per frame
void Update ()
{
attractor.Attract (myTransform);
}
}
Here the the package if you need : http://www.filedropper.com/gravity
" repulsive gravity"..."the player takes off" I don't understand, isn't this what we should EXPECT from anti-gravity? Or, is it supposed to push out from the CENTER of the bowl, pushing the character against the inside of the bowl? If so, is transform.position the center of the bowl?
Vector3 gravityUp = -(body.position - transform.position).normalized;
Why/how should the normal's of the bowl effect this force (as opposed to the rigid body's actual movement, which is handled by the engine)?
Answer by NeedNap · Sep 04, 2018 at 10:07 AM
Hi @Reivax41, do you have solved your problem? I have a similar problem: I created a "funnel" terrain where there is a ball (a rigid body) that falls inside this mesh but I would like that the ball stays attached to the mesh faces.
I want that the global gravity works, but I need an extra gravity that attracts the ball to the inner funnel faces.
If you have any suggestion for me it'll be appreciated.
I found this: https://github.com/JohannesDeml/adaptingGravityUnity3D I think it's a useful starting point.
Thank you
Answer by Reivax41 · Sep 17, 2015 at 07:38 PM
Currently the script pushing the character against the edge inside of the bowl. But despite of the mass and gravity scale, the character is light as a feather and tends to fly on long race.
I would use Raycast to get the ground normal and set it to the player. I think is more accurate. Or improve the existing code, but my knowledge is still quite limited. I don't know where to start.