- Home /
Moving a ball around a maze
Hi everyone,
I'm working on a maze-like game, whereby the player has to steer a ball without being detected by guards or cameras! The ball has a kinematic rigidbody and a sphere collider attached to it while the maze itself has a mesh collider attached to it. The problem is that whenever I move the ball towards the wall, the ball simply passes through! Shouldn't the engine automatically detect that there is a collision and stops the ball from passing through the wall? I tried various collision detection methods (continuous, discrete etc). I also tried switching off the isKinematic option but in the documentation there is specifically written that the position of rigidbodies can't be directly modified by transforms.
Should a script take care of this? Or this can be done natively within Unity? How should I go about this? Any help will be greatly appreciated! Thanks for your assistance.
Here is the simple script attached to the ball gameobject.
void Update () {
transform.Translate(0f, _floatHeight*Mathf.Sin(_floatFrequency*Time.time), 0f);
if(Input.GetKey(KeyCode.UpArrow))
transform.Translate(Vector3.forward * 0.5f);
if(Input.GetKey(KeyCode.DownArrow))
transform.Translate(Vector3.back * 0.5f);
if(Input.GetKey(KeyCode.LeftArrow))
transform.Translate(Vector3.left * 0.5f);
if(Input.GetKey(KeyCode.RightArrow))
transform.Translate(Vector3.right * 0.5f);
}
Answer by Waz · Aug 30, 2011 at 11:21 AM
You're using a Kinematic body. That means you decide it's position, not the physics engine, so it will happily let you embed it in a wall.
Answer by Uniquesone · Aug 30, 2011 at 11:26 AM
transform.Translate
Doesnt detect collisions..
Try to add a force or use transformdirection! http://unity3d.com/support/documentation/ScriptReference/Rigidbody.AddForce.html http://unity3d.com/support/documentation/ScriptReference/Transform.TransformDirection.html
if(Input.GetKey(KeyCode.UpArrow)) rigidbody.AddForce(Vector3.forward * 0.5f);
if(Input.GetKey(KeyCode.DownArrow)) rigidbody.AddForce(Vector3.forward * -0.5f);
if(Input.GetKey(KeyCode.LeftArrow)) rigidbody.AddForce(Vector3.right * -0.5f);
if(Input.GetKey(KeyCode.RightArrow)) rigidbody.AddForce(Vector3.right * 0.5f);
"I also tried switching off the is$$anonymous$$inematic option"
Then it should work =)
Answer by aldonaletto · Aug 30, 2011 at 01:33 PM
A kinematic rigidbody isn't under physics influence, as @Warwick Allison said. You should uncheck isKinematic and useGravity (since your ball floats). It's also better to store the initial height and add Mathf.Sin to it - if you use Translate, it can accumulate errors and after some time the ball may be landed or too high. Try this script:
public float _floatHeight = 0.1f; public float _floatFrequency = 3f; public float speed = 5f; float y0;
void Start(){ rigidbody.isKinematic = false; rigidbody.useGravity = false; y0 = transform.position.y; }
void Update() { Vector3 pos = transform.position; pos.y = y0+_floatHeight*Mathf.Sin(_floatFrequency*Time.time); transform.position = pos; Vector3 dir = new Vector3(Input.GetAxis("Horizontal"),0,Input.GetAxis("Vertical"))*speed; rigidbody.velocity = dir; // set the horizontal velocity according to controls }
Your answer
Follow this Question
Related Questions
Rigidbody necessary to use collision detection 1 Answer
Nav Mesh Agent won't fall off the platform? 1 Answer
Ball physics help 2 Answers
Character rigidbody collisions 1 Answer
Enemy bouncing physics 0 Answers