x


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);

}

more ▼

asked Aug 30 '11 at 10:37 AM

claytoncurmi gravatar image

claytoncurmi
11 5 6 8

(comments are locked)
10|3000 characters needed characters left

3 answers: sort oldest

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.

more ▼

answered Aug 30 '11 at 11:21 AM

Waz gravatar image

Waz
6.4k 22 33 70

(comments are locked)
10|3000 characters needed characters left

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);
more ▼

answered Aug 30 '11 at 11:26 AM

Uniquesone gravatar image

Uniquesone
86 9 14 14

You can't AddForce to a Kinematic Rigidbody.

Aug 30 '11 at 11:34 AM Waz

"I also tried switching off the isKinematic option"

Then it should work =)

Aug 30 '11 at 11:44 AM Uniquesone
(comments are locked)
10|3000 characters needed characters left

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
}
more ▼

answered Aug 30 '11 at 01:33 PM

aldonaletto gravatar image

aldonaletto
41.2k 16 42 195

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x2482
x1865
x1780
x168
x21

asked: Aug 30 '11 at 10:37 AM

Seen: 1213 times

Last Updated: Aug 30 '11 at 01:33 PM