how do i handle a character controller detection without using raycasts?

i have a character controller and an egg.
i want the egg to disapear when my character touches the egg.

how can i detect collision from the egg, without shooting raycasts?

thanks in advance!

 private void OnCollisionEnter(Collision collision)
    {
        if (collision.gameObject.CompareTag("Egg"))
        {
             
        }

Make sure you create a custom tag in the Unity Inspector and assign it to your Egg Prefab in your prefabs folder. From there just edit within the if statements brackets to cause the effect you want.

1 Like

I tried adding a “istrigger” object to the egg (added an invisible gameobject) (and, or player) and it didnt work, the collision works and the player does collide, but it wont detect it.

Thank you!
While it does work, it will have effect on all gameobjects that collide with the egg, i want to make it so only when the player collides the egg will do something.

Apply this to your player controller script. Then it will only compare the collision for the player and act only if the colliding object carries the “Egg” tag.

Oh wait wait wait. If you want the egg to do something you should do this –

private void OnCollisionEnter(Collision collision)
       {
              if (collision.gameObject.CompareTag("Player"))
              {
                      // Do stuff
              }
       }

Add this to your egg script and add a “Player” tag to the player GameObject.

EDIT – It really depends on what kind of stuff you want to do. If you want the egg to give a boost to the player or something, you’d want to compare the players collision to a colliding object carrying an egg tag. I

Meanwhile, if you want the egg to like animate itself or something, it would likely be better to add a script to the egg to compare its collisions to a gameObject carrying a player tag.

it doesnt work… its like the egg doesnt know the player exists.

here is my code:

private void OnCollisionEnter(Collision collision)
    {
        Debug.Log("collides");
        if (collision.gameObject.CompareTag("Player"))
        {
            Debug.Log("yay");
            gameObject.SetActive(false);
        }
    }

i only get one “collides” with the floor, but the “yay” never happen.

edit:
i can add this to the player, but it will make the player have million colliders check in the collision script so i want the check to be on the other game objects that check if the player collides with them.

  1. did your player has a collider?
  2. did your player has a tag call “Player”?
  3. what layer is the player have?
  4. what layer is the Egg have?
  5. did you set up your Physic Layer collision rules properly
    for example Edit/ProjectSettings/Physics
1 Like
bool triggerSwitch;
BoxCollider triggerCollider;
Vector3 triggerColliderSize;

void Start()
{
  triggerCollider = gameObject.AddComponent<BoxCollider>();
triggerCollider.isTrigger = true;
triggleCollider.size = new Vector3.zero + triggerColliderSize;
}

void Update()
{
   if (triggerSwitch)
       {
            print("Trigger");
       }
}



private void OnTriggerEnter(Collider collisionTrigger)
 {
          triggerSwitch = (collisionTrigger.transform.name == "player" || other.transform.name == "otherObject");
}

To keep it simple, triggerSwitch is true if collisionTrigger Collider variable is also true :slight_smile:
You can have the original collision to stop you egg falling through the ground and use this to create one and adjust it’s area sensing size. :slight_smile:

1 Like

i just added a capsule collider and it worked! thank you so much! <3

i think the character controller collider doesnt detect collision for some reason, its just there for the collision itself but there is no way to actualy get a coliision effect between a character controller and another object.