How to collide objects with high speed in Unity

后端 未结 2 1092
心在旅途
心在旅途 2020-12-18 23:54

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, an

相关标签:
2条回答
  • 2020-12-19 00:10

    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");
    
        }
    }
    
    0 讨论(0)
  • 2020-12-19 00:28

    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
    0 讨论(0)
提交回复
热议问题