0
\$\begingroup\$

I have a problem with rigidbody collisions atm. I have a parent object that has a rigidbody and I am spawning objects that become that parent object's child (the children are all spheres). These objects fly around the map and collide with the environment. At low speeds, these child objects do not fly through the floor. How do I fix this? These are the solutions that I have come up with and still have not worked:

  1. The environment is static.
  2. The parent rigidbody is using continuous dynamic collision detection.
  3. The parent rigidbody is interpolating its position.
  4. The children do raycast checks when moving so that they don't move through an object, but instead stop at the surface so that a collision can be detected.
// Detect collision with the environment so that the physics update can work
            RaycastHit hitInfo;
            bool raycastCollision = Physics.Raycast(missileManagerData.missiles[missileIndex].transform.position, 
                missileManagerData.missiles[missileIndex].transform.forward, out hitInfo, missileManagerData.missileCurrentSpeed[missileIndex] * Time.deltaTime);

            // Move rocket forward
            if (raycastCollision)
            {
                missileManagerData.missiles[missileIndex].transform.position = hitInfo.point;
            }
            else
            {
                missileManagerData.missiles[missileIndex].transform.position = missileManagerData.missiles[missileIndex].transform.position +
                    missileManagerData.missiles[missileIndex].transform.forward * missileManagerData.missileCurrentSpeed[missileIndex] * Time.deltaTime;
            } 

The biggest problem that I'm seeing is that the children have to be moved with transform.position = newPosition. Is there a way to do this through a physics update? I want to avoid having more rigidbodies as much as possible.

Edit: I've decided to add rigidbodies to all children and the same problem is present. The collision code has been transferred to each child instead of the parent. I don't know what to do anymore.

As a reference, here is the collision code:

// Method to perform all collisions for the missiles
        private void OnCollisionEnter(Collision collision)
        {
            missileCollisions.MissileCollisionsMain(missileManagerData, collision, missileUtilities);
        }
// Method to manage missile collisions
        public void MissileCollisionsMain(MissileManagerData missileManagerData, Collision collision, MissileUtilities missileUtilities)
        {
            Vector3 normals = Vector3.zero;
            for (int i = 0; i < collision.contactCount; i++)
                normals += collision.GetContact(i).normal;

            int missileIndex = collision.GetContact(0).thisCollider.gameObject.GetComponent<MissileData>().index;

            missileManagerData.missiles[missileIndex].transform.forward = Vector3.Reflect(missileManagerData.missiles[missileIndex].transform.forward, normals);


            if (collision.GetContact(0).otherCollider.gameObject.layer == 3)
            {
                missileUtilities.DestroyMissile(missileManagerData, collision.GetContact(0).thisCollider.gameObject);
            }
        }
```
\$\endgroup\$
4
  • \$\begingroup\$ As a test, have you tried to increase the size of the collider of your ground? \$\endgroup\$
    – Zibelas
    Commented Apr 3, 2023 at 6:39
  • \$\begingroup\$ @Zibelas I haven't tried increasing it more, but I increased it from a plane to a box collider \$\endgroup\$ Commented Apr 3, 2023 at 7:05
  • \$\begingroup\$ If the collider is too thin, with enough speed they can go through the collider without hitting it when the individual movement step is bigger than the collider. This would support why it is working on low speed but not on fast one \$\endgroup\$
    – Zibelas
    Commented Apr 3, 2023 at 7:45
  • \$\begingroup\$ @Zibelas I agree that is probably a solution, but I don't understand why that needs to be done when the collision detection is continuous dynamic, the scene is static, and I'm even interpolating the physics frames. I gave up on the project since some geometry may be fairly thin and I can't have this be a limiter. I looked it up and apparently Unreal has superior collision detection. It hurts as a Unity simp, but it looks like I have to convert... \$\endgroup\$ Commented Apr 4, 2023 at 3:54

0

You must log in to answer this question.

Browse other questions tagged .