- Home /
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#
Answer by Scribe · Aug 11, 2014 at 01:41 PM
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
Thanks for your quickly reply. Highly appreciated. I will check this code in a simple cube and will post the findings. Also, is it needed to have code for collision detection separately for the tunnel?
I'm a bit confused by your question, this should work as long as your tunnel has a collider and your object has a rigidbody!?
Hi there, sorry for the delayed reply. I did tried this script and the ball moves and spins everywhere on the surface. I Have added the tunnel mesh with collider. All I need is just the ball need to move inside the tunnel 360 degree. But now it moves everywhere without any proper control
Yes, object has a rigid body. I guess as per your advice this method will be more expensive in terms of memory consumption. However, is it possible to move just the tunnel? If so how to make collision happen? $$anonymous$$eaning the Tunnel mesh is curvy..
So in my setup the tunnel is not the actual collider, I have an invisible ring of box colliders ins$$anonymous$$d, as the inside of a tunnel is a concave shape, and generally collisions with concave colliders are either unreliable or expensive.
Then my ball has its rigidbody which will only ever collide with this set of box colliders, then the tunnel is not given a collider at all.
You can have a go on my quick 'n' dirty setup here if you want (A/leftArrow and D/rightArrow move the ball).
You can also download the package if you wish to take a closer look at some parts of it, the procedural tunnel generation might help as well!
Hope that helps you out,
Scribe