|
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 () { }
(comments are locked)
|
|
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.
(comments are locked)
|
|
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 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)
|
|
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
}
(comments are locked)
|
