3

I could really need some help with my GameObjects.

I am working on a game in which I want a pick-up Item to create a Physics Force explosion to blow away the enemies. I made a simple Bomb-Object to test this idea. I added a straightforward code, using a loop to collect all the colliders within its radius, to then addForce to these colliders. Now the code is working properly, but not all my GameObjects are reacting properly.

I looked into why this is, and I noticed that my Collider keeps falling away from the GameObject as soon as I add a RigidBody component to the object. The RigidBody is needed for the AddForce impact. When I remove the rb, the collider stays in place, but the object does not react to the Physics Force.

I added some images to illustrate what I mean:

Image of the Collider of the leaf sinking away..

Example image of objects which DO react to the AddForce.

I already tried to:

  • Copy/Paste all component settings from the reacting Cube and stone Gameobjects to the Leaf Gameobject while disabling all other code such as the c# scripts. The Collider & RB do not fall through the floor but when Physics Force hits the Collider is blown away while the GameObject keeps its position.
    • Try different GameObjects/Collider types.
    • Removed the water.
    • Play with amount of force/radius of the bomb.
    • Edited 'Tag' and 'Layerstyle' of GameObject.

The 'Explosion Code' is added below, however, I don't think the problem is in the code. I added the code to the Main Camera of my scene, and added a simple sphere as the 'bomb' GameObject.


using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Explosion : MonoBehaviour
{
    public GameObject bomb; //set the position of the explosion
    public float power = 10.0f;
    public float radius = 10.0f;
    public float upForce = 0.0f;

    private void FixedUpdate()
    {
        if (Input.GetKeyDown("space"))
        {
            print("space key was pressed");
            Invoke("Detonate", 1);
        }
    }

    void Detonate()
    {
        Vector3 explosionPosition = bomb.transform.position; //set the position of our explosion to the position of the bomb.
        Collider[] colliders = Physics.OverlapSphere(explosionPosition, radius); //collects all colliders within the radius.
        foreach (Collider hit in colliders) { //for each individual collider the following code is ran.

            Rigidbody rb = hit.GetComponent<Rigidbody>(); //declare'rb' rigidbody. Get rb component from each collider
            if (rb != null)
            {
                print("BOOM!");
                rb.AddExplosionForce(power, explosionPosition, radius, upForce, ForceMode.Impulse); //add force to each collider
            }
        }

    }
}


How do I make the Rigidbody, Collider and GameObject of the leaf hold onto each other like the standard 3D object 'cube', so that I can make these blow away with the Physics Force just like the other models?

Thank you for your time, I have been trying things and looking around on the Internet for hours now but can't seem to find any solution.

4
  • What happens if you use a mesh collider instead of a sphere collider for the leaf? It may be that the center of you model is off-set, making the origin of the collider not be on your actual model. (However that should already happen w/o a rb then). Also what happens if you increase the center Y value on your collider to match your leaf again? Does it fall down automatically again or stay in place?
    – Remy
    Feb 2, 2019 at 16:54
  • Thank you for the reply. The mesh collider did not make any difference but changing the Y value on the collider to match the leaf made the collider stop falling through the floor, which is a big win! However, the collider still does not stick to the GameObject. When I activate the code the collider responds to the AddForce but the GameObject does not move, unlike the sphere object. I added an image through this link to illustrate. (pro2-bar.myportfolio.com/v1/assets/…) Feb 3, 2019 at 9:58
  • So I just added a mesh collider to another lotus leaf object, and the same thing happens. I have to press 'convex' or the lotus falls through the floor. However, the collider is still not attached to the GameObject properly as the collider moves out of place when I swim against the GameObject. (pro2-bar.myportfolio.com/v1/assets/…) Feb 3, 2019 at 10:08
  • That is very strange. When the collider moves but the lotus/leaf stays in position, does the XYZ of the transform change at all? Or does the center XYZ on the collider change?. Are there any scripts on your leaf that are not on your rock that does behave as expected? If so try disabling that script. Maybe there is another script working that locks the position of the object (also which version of Unity are you using?)
    – Remy
    Feb 3, 2019 at 12:39

1 Answer 1

0

What happens if you add the rigidbody in stop mode and press play? Does it move away in a similar manner? This may, and is expected to happen, if your colliders intersect with each other. As soon as you add a rigidbody, they find themselves trapped in a serious collision.

If you don't want to modify the scene, you can fiddle with the collision matrix in project settings / physics

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.