13

I try to create game for Android and I have problem with high speed objects, they don't wanna to collide.

I have Sphere with Sphere Collider and Bouncy material, and RigidBody with this param (Gravity=false, Interpolate=Interpolate, Collision Detection = Continuous Dynamic)

Also I have 3 walls with Box Collider and Bouncy material.

This is my code for Sphere

function IncreaseBallVelocity() {
rigidbody.velocity *= 1.05;
}

function Awake () {
rigidbody.AddForce(4, 4, 0, ForceMode.Impulse);

InvokeRepeating("IncreaseBallVelocity", 2, 2);
}

In project Settings I set: "Min Penetration For Penalty Force"=0.001, "Solver Interation Count"=50

When I play on the start it work fine (it bounces) but when speed go to high, Sphere just passes the wall.

Can anyone help me?

Thanks.

Edited

var hit : RaycastHit;

var mainGameScript : MainGame;

var particles_splash : GameObject;

function Awake () {
rigidbody.AddForce(4, 4, 0, ForceMode.Impulse);

InvokeRepeating("IncreaseBallVelocity", 2, 2);
}

function Update() {
if (rigidbody.SweepTest(transform.forward, hit, 0.5))
    Debug.Log(hit.distance + "mts distance to obstacle");
if(transform.position.y < -3) {
    mainGameScript.GameOver();
    //Application.LoadLevel("Menu");
}
}

function IncreaseBallVelocity() {
rigidbody.velocity *= 1.05;
}

function OnCollisionEnter(collision : Collision) {
Instantiate(particles_splash, transform.position, transform.rotation);
}

EDITED added more info

  1. Fixed Timestep = 0.02 Maximum Allowed Tir = 0.333
  2. There is no difference between running the game in editor player and on Android
  3. No. It looks OK when I set 0.01
  4. My Paddle is Box Collider without Rigidbody, walls are the same
  5. There are all in same layer (when speed is normal it all works) value in PhysicsManager are the default (same like in image) exept "Solver Interation Co..." = 50
  6. No. When I change speed it pass other wall
  7. I am using standard cube but I expand/shrink it to fit my screen and other objects, when I expand wall more then it's OK it bouncing
  8. No. It's simple project simple example from Video http://www.youtube.com/watch?v=edfd1HJmKPY
  9. I don't use gravity
Kec
  • 1,435
  • 4
  • 16
  • 21

2 Answers2

4

See:

  1. Similar SO Question
  2. A community script that uses ray tracing to help manage fast objects
  3. UnityAnswers post leading to the script in (2)

You could also try changing the fixed time step for physics. The smaller this value, the more times Unity calculates the physics of a scene. But be warned, making this value too small, say <= 0.005, will likely result in an unstable game, especially on a portable device.

The script above is best for bullets or small objects. You can manually force rigid body collisions tests:

public class example : MonoBehaviour {
    public RaycastHit hit;
    void Update() {
        if (rigidbody.SweepTest(transform.forward, out hit, 10))
            Debug.Log(hit.distance + "mts distance to obstacle");

    }
}
Community
  • 1
  • 1
Jerdak
  • 3,997
  • 1
  • 24
  • 36
  • I try to set fixed time step for physics 0.01 and it worked, now Sphere can stand more but when it speed up I have same problem. I try DontGoThroughThings but I'm not sure is this properly way, I just copy code in my Sphere JS script, do I need to setup something more? – Kec Jan 14 '13 at 23:21
  • @Kec No, as long as your sphere has a rigidbody component. The script is only useful for very rough tests and it uses what is likely the center of your object for testing. But since you have a sphere you could easily ray cast out, the the nearest hit, and if that hit is < the radius of your sphere, stop the sphere. – Jerdak Jan 15 '13 at 00:53
  • I try various things but it's not working. Now I have this code so can you tell me what to change. I add code to question. Thanks for help. – Kec Jan 15 '13 at 19:00
2

I think the main problem is the manipulation of Rigidbody's velocity. I would try the following to solve the problem.

  1. Redesign your code to ensure that IncreaseBallVelocity and every other manipulation of Rigidbody is called within FixedUpdate. Check that there are no other manipulations to Transform.position.
  2. Try to replace setting velocity directly by using AddForce or similar methods so the physics engine has a higher chance to calculate all dependencies.
  3. If there are more items (main player character, ...) involved related to the physics calculation, ensure that their code runs in FixedUpdate too.

Another point I stumbled upon were meshes that are scaled very much. Having a GameObject with scale <= 0.01 or >= 100 has definitely a negative impact on physics calculation. According to the docs and this Unity forum entry from one of the gurus you should avoid Transform.scale values != 1

Still not happy? OK then the next test is starting with high velocities but no acceleration. At this phase we want to know, if the high velocity itself or the acceleration is to blame for the problem. It would be interesting to know the velocities' values at which the physics engine starts to fail - please post them so that we can compare them.


EDIT: Some more things to investigate
6.7 m/sec does not sound that much so that I guess there is a special reason or a combination of reasons why things go wrong.

  • Is your Maximum Allowed Timestep high enough? For testing I suggest 5 to 10x Fixed Timestep. Note that this might kill the frame rate but that can be dfixed later.
  • Is there any difference between running the game in editor player and on Android?
  • Did you notice any drops in frame rate because of the 0.01 FixedTimestep? This would indicate that the physics engine might be in trouble.
  • Could it be that there are static colliders (objects having a collider but no Rigidbody) that are moved around or manipulated otherwise? This would cause heavy recalculations within PhysX.
  • What about the layers: Are all walls on the same layer resp. are the involved layers are configured appropriately in collision detection matrix?
  • Does the no-bounce effect always happen at the same wall? If so, can you just copy the 1st wall and put it in place of the second one to see if there is something wrong with this specific wall.
  • If not to much effort, I would try to set up some standard cubes as walls just to be sure that transform.scale is not to blame for it (I made really bad experience with this).
  • Do you manipulate gravity or TimeManager.timeScale from within a script?
  • BTW: are you using gravity? (Should be no problem just
Kay
  • 12,918
  • 4
  • 55
  • 77
  • Ok, I have done some testing and this is a result: 1. When I set Velocity=6.7 then Ball is bounce in first wall but it pass second, in that case method OnCollisionEnter is call but bouncing not working, maybe it something wrong with Bouncy material which I use? 2. When I increase wall width or ball radius then is good, bouncing working. All manipulation of Rigidbody is called within FixedUpdate and I don't use IncreaseBallVelocity, I just set rigidbody.AddForce(6.7, 6.7, 0, ForceMode.Impulse); – Kec Jan 20 '13 at 18:10
  • I set scale parameters of the ball in the beginning when I create ball and that's all. But I am more interested in this, that it calls OnCollisionEnter but there is no bouncing, like something is not right with "Bouncy" material. – Kec Jan 21 '13 at 18:33
  • @Kec Well `Bounciness = 1` and `Bounce Combine = Max` should do. About the scaling: According to the [docs](http://docs.unity3d.com/Documentation/Components/class-Transform.html) and the [forum entry from one of the gurus](http://forum.unity3d.com/threads/105468-Mesh-scale-factor-vs-Transform-scale) you should avoid Transform.scale values != 1. – Kay Jan 21 '13 at 19:47
  • Parameters Bounciness = 1 and Bounce Combine = Max are already been set. And about scaling I set ball to be 1, 1, 1 but other objects I had to scale because symmetry, in that case I had to increase ball speed and then it happening again (ball pass the wall)? Any other idea? Thanks for help. – Kec Jan 22 '13 at 18:46
  • @Kec Tough problem but interesting. I updated my answer with some more suggestions and tweeted it - let's see. – Kay Jan 22 '13 at 19:52
  • When I attach Rigitbody to walls and check "Is Kinematic" and set Collirion Detection = Continuous then it stuck in the wall. – Kec Jan 23 '13 at 20:40