#Localised gravity to curved mesh

1 messages · Page 1 of 1 (latest)

barren eagle
#

Hello, I'm trying to make a project for the GMT jam and running into some issues with the core concept. I want to remain vague to hide the theme of my project, but the basic logic is I want the player object to stick to a mesh as it curves. Currently I'm adding rigidbody force to the object and rotating as it goes, but when the player is upside down it breaks, and it's very jittery while moving, it just looks awful and I'm not sure what to do.

`void FixedUpdate()
{
UpdateSurfaceNormal();
ApplyGravity();
AlignVisual();
HandleMovement();
}

void UpdateSurfaceNormal()
{
Ray ray = new Ray(transform.position, -transform.up);
if (Physics.Raycast(ray, out RaycastHit hit, 5f, trackLayer))
{
surfaceNormal = hit.normal;
}
}

void ApplyGravity()
{
rb.AddForce(-surfaceNormal * gravityStrength, ForceMode.Acceleration);
}

void AlignVisual()
{
// Smoothly rotate visual to align with surface
Quaternion targetRot = Quaternion.FromToRotation(carVisual.up, surfaceNormal) * carVisual.rotation;
visual.rotation = Quaternion.Slerp(carVisual.rotation, targetRot, 10f * Time.fixedDeltaTime);
}`

haughty kite
#

Maybe apply torque/angular velocity to make sure the object rotation aligns with the surface as well.
Other than that, there's not much to work with...🤷‍♂️