Gravity after player get hit

even after having set Mesh Collider component on the game object for the ground, the player can walk around on it but when get hit It falls down through the terrain…


What should I fix??
Also, the ground have Mesh Collider component…

Hi!

What collider do the balls have? Does the box also have a mesh collider on it?

The balls have sphere collider, box also have a box collider

This might be obvious, but double check that your collider on the ground is not set to trigger. For debugging purposes, I would also run a check inside of OnCollisionEnter() and print the collision object name to check if it is working as it should there. If you have coded any collision related behaviour (such as what happens to the player after being hit, you should include that too.

1 Like

make sure your yellow box isn’t partially submerged in the ground before the hit

1 Like

The script which attached to the projectile object is:


void OnCollisionEnter(Collision collision)
    {
        // string nomeCheColpisce = collision.gameObject.tag;   // l'altro oggetto
        // string nomeColpito = this.gameObject.tag;            // questo oggetto
        string nomeCheColpisce = this.gameObject.tag; //   // l'altro oggetto
        string nomeColpito =  collision.gameObject.tag;
        Debug.Log("Oggetto che colpisce: " + nomeCheColpisce);
        Debug.Log("Oggetto colpito: " + nomeColpito);

        if (nomeColpito == "Player") //was collision.gameObject.tag == "Player"
        {
            GetComponent<MeshRenderer>().material.color = Color.magenta;
            gameObject.tag = "Hit";
            Debug.Log("- hit by projectile !!!!!XD" ); 
            
        }
        
         
    }

Yes, It is not submerged

mmm, I got to attach this script to the bullet:


void OnCollisionEnter(Collision collision)
    {
        
        string nomeCheColpisce = this.gameObject.tag; //   // l'altro oggetto
        string nomeColpito =  collision.gameObject.tag;
        
        if (nomeColpito == "Player") //was collision.gameObject.tag == "Player"
        {
            GetComponent<MeshRenderer>().material.color = Color.magenta;
            gameObject.tag = "Hit";
            Vector3 pos = collision.gameObject.transform.position; // item get hit position vector
            pos.y = 0.55f; // on terrain position
            collision.gameObject.transform.position = pos;
            Debug.Log("reset position on terrain !!!!!!");
            
        }
        
         
    }

after having applied this yet, the player (the cube) drop through terrain when get hit… :thinking:

another thing to check is layer overrides on the collider of the player and terrain
image
Then confirm they are not excluding each other’s layer.
image