Bump score at start of game

I noticed the same thing. At the start of each game I try, the score is automatically incremented to 1. After a little investigation I found that my player was touching the ground so that when the game started, it took the ground touching as a HIT! It is not set up as a collide object but scores it anyway. I SOLVED it by picking the player up and putting it just above the ground plane. BINGO! No more extra Hit score when starting the game.

This is just for information to anyone else that has the problem. I’m sure things like this will be address in future lectures and there are probably better ways of setting up a player character.

14 Likes

Thanks for this, It was driving me up the walls.

4 Likes

I also had that but I did another thing.
If you don’t want to move the player up, you can disable the Mesh Collider on the ground plane. (But it will make the falling cube go through the ground later, so that’s a problem)

1 Like

I solved this by making int hits = -1; So now when the player hits the floor, the hits start at zero. There probably is a better solution though^^.

5 Likes

Pretty new to Unity development still, but you could probably use tags to fix this. Make the walls (or bumpable objects) have a tag and check if the object you are colliding into, has the tag.

Noticed the same thing and at first I thought I programmed it wrong, then I figured, the floor has the mesh collider too and fixed it easy. For now I just moved my player a little above the floor, but I would say using tags is a nicer fix.

1 Like

You could also use collision layers to solve which objects are allowed to collide. That being said, with the player object’s Y-transform and all rotation frozen, a little bump above the ground is certainly the simplest solution atm.

Cheers for the fix! Glad it wasn’t just me having this issue haha.

Another method to fix this is by the following, this would also let you extend it to add jumping later and not increment with floor collisions:

    // Check if object is named Floor (or whatever you named the floor object in your scene)
    if (collision.gameObject.name == "Floor")
    {
        Debug.Log("You've hit " + collision.collider + " this does not count to score!");
        
    }
    // If it does not count it to score
    else
    {
        score++;
        Debug.Log("You've hit " + collision.collider + " you hit something " + score + " times!");
    }
2 Likes

I used this method, but i was really annoyed it would say you hit some thing 0 times when only the floor collision had happened, so this was my solution:

{
private int collisionCounter = -1;
private bool allowCounter = false;

private void OnCollisonEnter():
collisionCounter++;

 if (allowCounter)
 {
      Debug.Log("You've hit an object "  + collisionCounter + "times")
 }

 allowCounter = true;

}

The boolean stops the display message the first time but allows it every other time. I didn’t want to raise the player over as I thought it was a less proper solution.

1 Like

This fixed my issue :slight_smile:

As well notice that Rick has his “Dodgy” player :wink: within the floor itself (very beggining of the course) as well as all other objects are slightly touching the floor. By moving the player to be slightly within the floor doesn’t cout towards a collision event. :smiley:

I had the same issue. Since we never want to increment when hitting the floor. I change my collision function to be this:

   private void OnCollisionEnter(Collision other) 
    {
        if (other.gameObject.name != "floor"){
            hits++;
            Debug.Log("You've bumped into something this many times: " + hits);
        }
    }

This method just states if I am not ( != )hitting the floor object then increment. This way I don’t have to worry about extra math, additional variables, or worry about complications with meshes.

3 Likes

ended up doing something similar to Scott above, added a “Player” tag to the Player object (Top of the Inspector, just below object name). Then added a line to the ObjectHit script. Probably not optimal but I can’t really code but it also works with the falling cube later on, and is easy to understand:

    private void OnCollisionEnter(Collision other)
    {
        if (other.gameObject.tag == "Player")  // <Adds a score only if hit by a player, ignoring the floor
        {GetComponent<MeshRenderer>().material.color = Color.magenta;}
    }

Thank you so much for this. I couldn’t figure it out and I couldn’t see anything wrong with my code

Thanks for noticing that a collision with the floor counts as a hit. Disabling the mesh collider of the floor fixes this problem.

Privacy & Terms