Third Person Controller not detecting collision with Sphere GameObjects

Hi all

I have this script attached to the Unity Starter Asset Third Person Controller.
I have a scene with some Sphere GameObjects and when the controller collides with them, they are supposed to be destroyed.
The OnCollisionEnter method doesn’t seem to be executed.

There is a projectile fired at the player and when it hits the controller, the OnCollisionEnter method is executed and the Print message is displayed.
Why is this happening but the not when the controller collides with a Sphere GameObject?
Thanks!

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Rendering.Universal;

public class CollisionWithPlayer : MonoBehaviour
{
    // Start is called before the first frame update
    int score;

    void Start()
    {
      
        score = 0;

    }

    // Update is called once per frame
    void Update()
    {
      
    }
     void OnCollisionEnter(Collision collision)
    {
        Debug.Log("Collison");
        print("Fucking Collided with " + collision.collider.gameObject.tag);

        if (collision.collider.gameObject.CompareTag("Sphere"))
        {
            Destroy (collision.collider.gameObject); 
            score++;
            print("Score" + score);



        }
    }
}

Time to start debugging! Here is how you can begin your exciting new debugging adventures:

You must find a way to get the information you need in order to reason about what the problem is.

Once you understand what the problem is, you may begin to reason about a solution to the problem.

What is often happening in these cases is one of the following:

  • the code you think is executing is not actually executing at all
  • the code is executing far EARLIER or LATER than you think
  • the code is executing far LESS OFTEN than you think
  • the code is executing far MORE OFTEN than you think
  • the code is executing on another GameObject than you think it is
  • you’re getting an error or warning and you haven’t noticed it in the console window

To help gain more insight into your problem, I recommend liberally sprinkling Debug.Log() statements through your code to display information in realtime.

Doing this should help you answer these types of questions:

  • is this code even running? which parts are running? how often does it run? what order does it run in?
  • what are the names of the GameObjects or Components involved?
  • what are the values of the variables involved? Are they initialized? Are the values reasonable?
    - are you meeting ALL the requirements to receive callbacks such as triggers / colliders (review the documentation)

Knowing this information will help you reason about the behavior you are seeing.

You can also supply a second argument to Debug.Log() and when you click the message, it will highlight the object in scene, such as Debug.Log("Problem!",this);

If your problem would benefit from in-scene or in-game visualization, Debug.DrawRay() or Debug.DrawLine() can help you visualize things like rays (used in raycasting) or distances.

You can also call Debug.Break() to pause the Editor when certain interesting pieces of code run, and then study the scene manually, looking for all the parts, where they are, what scripts are on them, etc.

You can also call GameObject.CreatePrimitive() to emplace debug-marker-ish objects in the scene at runtime.

You could also just display various important quantities in UI Text elements to watch them change as you play the game.

Visit Google for how to see console output from builds. If you are running a mobile device you can also view the console output. Google for how on your particular mobile target, such as this answer for iOS: https://discussions.unity.com/t/700551 or this answer for Android: https://discussions.unity.com/t/699654

If you are working in VR, it might be useful to make your on onscreen log output, or integrate one from the asset store, so you can see what is happening as you operate your software.

Another useful approach is to temporarily strip out everything besides what is necessary to prove your issue. This can simplify and isolate compounding effects of other items in your scene or prefab.

If your problem is with OnCollision-type functions, print the name of what is passed in!

Here’s an example of putting in a laser-focused Debug.Log() and how that can save you a TON of time wallowing around speculating what might be going wrong:

https://discussions.unity.com/t/839300/3

“When in doubt, print it out!™” - Kurt Dekker (and many others)

Note: the print() function is an alias for Debug.Log() provided by the MonoBehaviour class.

Ummm, okay, thanks, I think :slight_smile:

If I remember correctly, with object to object collision, both objects need a collider, and one of them needs a rigidbody

See the collision graph on this page. It shows what needs to happen in order for a collision event to occur.

Does the new Third Person Controller have a collider attached, as I don’t see one in the inspector.

yeah, technically, it depends on which setup you have. As the documentation states:
With normal, non-trigger collisions, there is an additional detail that at least one of the objects involved must have a non-kinematic Rigidbody (ie, Is Kinematic must be switched off). If both objects are kinematic Rigidbodies then OnCollisionEnter, etc, will not be called. With trigger collisions, this restriction doesn’t apply and so both kinematic and non-kinematic Rigidbodies will prompt a call to OnTriggerEnter when they enter a trigger collider.

So yes, 2 colliders are needed for sure, in any situation. If your player object doesn’t have any collider on it, whether box, sphere, capsule or mesh, then it won’t activate any collision.

Sorry, I have no clue, I make all my own methods from camera to inputs. Hopefully someone knows more about pre-built methods.

Well I’ve added a capsule collider to the third person controller. I’ve added a rigidbody to the sphere, still the collision isn’t detected.

In this tutorial example, from the Unity To Profiency Intermediate book, there is a sphere projectile fired at the controller.
The projectile does cause the OnCollisionEnter method to at least print the message.
It’s strange that walking into the test sphere with the same components as the projectile doesn’t work.
The only difference I can see is that the projectile has a AddForce component

I haven’t used it, but I’m guessing if you don’t see a collider then it probably has a CharacterController component attached. A CharacterController is a modified capsule collider. In that case, you probably need to use OnControllerColliderHit.

https://docs.unity3d.com/ScriptReference/CharacterController.OnControllerColliderHit.html