OnCollisionEnter detecting mutliple collisions per hit

Completely new to unity and coding and I’m having trouble figuring this out on my own. I am at the “incrementing a score” lecture for on the unity 3d course.

Any script I add for “OnCollisionEnter” ends up detecting mass amounts of hits for any bump. Meaning I bump a wall and get 10-20 messages in the debug.log. (regardless of whether the script is on the object or player)

In the first lessons it was noted that you could keep pressing forward against the wall and only register one hit, but this was not the case for me. Holding forward continues to add dozens of messages to the console. I figured I would understand more by this point to be able “fix” the issue on my own, but now implementing a “scorer” script that is counting mass amounts of imprecise hits, seems like it will compound problems later.

Any insight anyone could provide would be super appreciated, I can provide more info if needed. I’ve pasted my relevant scripts below, including my “Mover” script which I modified slightly to suit a “vehicle” as the “player.” This is currently the only script that has been modified from the course instruction but the modification was only to make the player rotate instead of sliding horizontally and only rotate the player if moving forward or backward.

My “ObjectHit” script

public class ObjectHit : MonoBehaviour
{
    private void OnCollisionEnter(Collision other) 
    {
        Debug.Log("Crash Happened");
        GetComponent<MeshRenderer>().material.color = Color.red;
    }
}

My “Scorer” script

public class Scorer : MonoBehaviour

{

    private void OnCollisionEnter(Collision other) 

    {

        Debug.Log ("You crashed");

    }

}

My “Mover” script

public class Mover : MonoBehaviour
{
    [SerializeField] float moveSpeed = 10f;
    [SerializeField] float rotateSpeed = 200f;

    // Start is called before the first frame update
    void Start()
    {
        PrintInstructions();
    }

    // Update is called once per frame
    void Update()
    {
        MovePlayer();
    }

void PrintInstructions()
{
    Debug.Log("Welcome to the Game");
    Debug.Log("Keyboard? Use WASD");
    Debug.Log("Controller? Right and Left trigger = Gas/Brakes. Left stick = Turn");
    Debug.Log("Don't Crash!");
}

void MovePlayer()
{
    
    float zValue = Input.GetAxis ("Vertical") * moveSpeed * Time.deltaTime;
    float yValue = Input.GetAxis ("Horizontal") * rotateSpeed * Time.deltaTime;
    transform.Translate(0,0,zValue);
    //Only rotate if moving
    if( Input.GetAxis("Vertical") != 0)
    transform.Rotate(0f, yValue, 0f);   

}

}

That might happen because of several issues, some of them might get fixed after lecture 25 of the course, some won’t, you might want to continue until that point, if the issue persists come back to this thread and check the possible solutions:

Reasons causing your issue that will get fixed after lecture 25:

  • Multiple colliders in the object or child objects. If you created a vehicle with multiple colliders that might be causing the issue.
  • Using transform.Translate to move objects. That method teleports the object. As Rick pointed out, it isn’t suitable for physics nor collisions. It will behave quite differently from computer to computer, in some cases it will completely ignore collisions even with a Rigidbody attached. It might cause unexpected behaviors like the one you described.

Reasons that won’t get fixed after lecture 25.

  • Having multiple objects at the same point.
  • Multiple ‘Scorer’ scripts in the same object or multiple ‘ObjectHit’ in the same object.

A really nice way to check for those is to print the name of the objects that are colliding in the Console, for that you’ll need to use transform.name.

Hope this helps.

1 Like

Thank you!!! Problem solved.

The problem was multiple colliders in child objects. I assumed applying the script to the parent object with its own custom collider meant it would not apply to all child objects within. Good to know that it indeed will apply.

I think I missed some of Rick’s specific notes about transform.Translate having physics related drawbacks but I imagine this is the reason I’ve been encountering oddities and errors trying to impart momentum onto the player character. All really good to know, thanks for the help.

1 Like

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms