Hey all
I'm creating a game, where a ball is moving inside a tube. The ball should have the possible of moving along the inside of the tube all the way around, like it is stuck to the wall.

I have made a sample in 2D, which you can see a sketch of here. This shows a ball which is in the top half of the circle. Under normal gravity (one way gravity) it would fall down, but in my case, the gravity force should force the ball to stay close to the wall. The gravity axis is the negative normal vector in this case.
So, my problem is, that to ball won't stop bounce as soon as it hits the wall. The gravity axis is being set correctly..
I have based my gravity system a little on the FauxGravity.
Here is my code for the ball:
using UnityEngine;
using System.Collections;
[RequireComponent(typeof(Rigidbody))]
public class GravityForce : MonoBehaviour
{
// are we touching the surface?
private int grounded = 0;
// Set to true for mono-directional gravity
private bool useLocalUpVector = false;
// Force applied along gravity up-vector (negative = down)
private float fauxGravity = 0.5f;
private float speed = 2f;
void Start()
{
rigidbody.WakeUp();
rigidbody.useGravity = false;
}
void FixedUpdate()
{
Vector3 gravityUp;
Vector3 tangent;
Vector3 moveDirection = new Vector3(0f, 0f, 0f);
// Figure out the body's up vector
if (useLocalUpVector)
{
gravityUp = transform.up;
}
else
{
RaycastHit hit;
bool hitData = Physics.Raycast(transform.position, -transform.up, out hit);
gravityUp = new Vector3(0f, 1f, 0f);
if (hitData)
{
if (hit.distance < 0.1) {
grounded++;
Debug.DrawRay(hit.point, hit.normal, Color.cyan);
gravityUp = -hit.normal.normalized;
tangent = Vector3.Cross(hit.collider.transform.right, hit.normal).normalized;
Debug.DrawRay(hit.point, tangent, Color.magenta);
Debug.Log("gravityUp: " + gravityUp);
}
}
gravityUp.Normalize();
}
// Accelerate the body along its up vector
Vector3 v = gravityUp;
rigidbody.AddForce(new Vector3(v.x, v.y, 0) * fauxGravity * rigidbody.mass);
rigidbody.drag = (grounded > 0) ? 1.0f : 0.1f;
}
}
Unity package sample (zip-file)
I hope you can help :-S