Moving a Simple Ball [Main Player!] inside a tunnel without gravity!

Am planning to build a game which involves making a simple Ball as a main character and it will move inside a Tunnel. The player can Move forward, Left and Right. Also the player can basically go 360 degree inside the tunnel. So, it will not have a gravity system.

Collision Detection need to be High as the game move forwards the speed increases. The interested thing about this game is it will be fast and player can run through 360 degree in the tunnel.

I would highly appreciate for your valuable input and time. Thanks in advance.

P.S: I use Unity c#

Hey there, are you sure you actually need physics to do this? Often for this type of thing you would keep the ball in the same z position and move or create a tunnel around it.

I have set up a simplish script which you can see below, which will work given that you can find the correct ‘centre’ position, however if you are moving the ball, especially in a changing direction (a curved tunnel) finding the centre from which to apply gravity becomes more difficult. In that case you will have to code a slightly more complicated and more expensive (computationally) script where you find the normal of the collision between the ball and tunnel surface and apply gravity in the opposite direction of that. You can get the normals of the contact points from OnCollisionStay

Here is a more trivial approach for simplified cases!

float gMag = 9.8f;
float moveForce = 5f;
Vector3 centre = Vector3.zero;
private Vector2 gDir = Vector3.zero;
private Vector2 gPer = Vector3.zero;

void FixedUpdate () {
	gDir = transform.position == centre ? -Vector3.up : (transform.position - centre).normalized;
	gPer = new Vector2(gDir.y, -gDir.x);
	
	rigidbody.AddForce(gDir*gMag, ForceMode.Acceleration);
	if(Input.GetKey(KeyCode.RightArrow)){
		rigidbody.AddForce(-gPer*moveForce);
	}else if(Input.GetKey(KeyCode.LeftArrow)){
		rigidbody.AddForce(gPer*moveForce);
	}
}

Hope that gets you started!

Scribe