Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Character OnCollisionEnter

Discussion in 'Scripting' started by Ryder11, May 15, 2009.

  1. Ryder11

    Ryder11

    Joined:
    May 1, 2009
    Posts:
    7
    I'm trying to detect when my character collides with a cube. The cube is not moving, it's just sitting there but I want to know when the player runs into it, or jumps on it.

    The character is the First Person Controller prefab.

    The box is just a cube with a box collider, rigidbody, and my script attached. The rigidbody has use gravity on and is kinematic off. I've tried turning on is kinematic, that doesn't help.

    Here is the code I'm using:

    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Collide : MonoBehaviour {
    5.  
    6.     void OnCollisionEnter(Collision collision) {
    7.         Debug.Log("Collision");
    8.     }
    9. }
    If I place the cube above the floor the collision happens when it lands, on the floor or on my character...but when it's just sitting there and my character jumps on it...nothing.

    I know I can do it turning isTrigger on and using OnTriggerEnter but I do not want the character to pass through it.

    I've seen this question asked plenty of times, left un-answered!

    I thought I'd try this time.

    Thanks
     
  2. Hermit

    Hermit

    Joined:
    Mar 27, 2009
    Posts:
    45
    I did your test, except using UnityScript (?) instead of C+...

    Code (csharp):
    1. function OnCollisionEnter(collisionInfo:Collision) {
    2.     if(collisionInfo.gameObject.tag == "first"){
    3.     Debug.Log("Collide with FPS");
    4.     }
    5.     Debug.Log("Collision");
    6. }
    and got the exact same results. So I started messing with different settings, and discovered that if you go into the FPController/Graphics/Capsule Collider and change the radius up a bit, it works fine.

    Seems to be something about the two rigidbodys not normally connecting, unless there's enough force to get through the mesh, say, the force of the falling Cube.

    HTH
     
  3. Ryder11

    Ryder11

    Joined:
    May 1, 2009
    Posts:
    7
    I tried that and still no luck.

    By default the FPController prefab doesn't have a capsule collider because its parent GO has a character controller which handles the collisions. I added one just to test and made the radius larger then the other controller, still nothing.

    Does the character controller not work with OnCollisionEnter?

    I've also tried OnControllerColliderHit and attached it to the player, but it behaves more like OnCollisionStay, where its called multiple times as long as the player is touching it.

    That doesn't work the way I want it. I would also like the script to be attached to the GO you're going to be hitting and not the player.

    Anyone? Someone has to have this working...
     
  4. sinu

    sinu

    Joined:
    Jan 28, 2009
    Posts:
    65
  5. Ryder11

    Ryder11

    Joined:
    May 1, 2009
    Posts:
    7
    Like I said I had it working using OnControllerColliderHit but it functions more like OnCollisionStay vs OnCollisionEnter. I only want it called once when the object is bumped, not called for as long as the player is touching it.

    I would also like to have the collision script on the object being hit and not the player.

    If it's not possible...which seems strange to me...I will have to do it that way I guess
     
  6. sinu

    sinu

    Joined:
    Jan 28, 2009
    Posts:
    65
    Sorry, my mistake, just browsed the post and didn't read it in detail.

    I think the character controller functions are a bit limited in that respect so you would have to make a workaround using a rigidbody unless someone know another way.
     
  7. Bunzaga

    Bunzaga

    Joined:
    Jan 9, 2009
    Posts:
    202
    Add a second collider to the character object which is just a trigger collider, and use that instead.
     
  8. Tubeliar

    Tubeliar

    Joined:
    May 21, 2009
    Posts:
    14
    You could also add an empty game object with a trigger collider that sticks out a bit above your box. This will send trigger messages to the player.

    Or you could add two booleans to your player script, isTouching and wasTouching. OnControllerColliderHit always sets isTouching to true. In your update() you check if isTouching == true wasTouching == false, then you have jumped on the box. At the end of update you first set wasTouching = isTouching and then isTouching = false. If you're still standing on the box then isTouching will be set to true before the next update and inside the update function it will seem like nothing changed. If the player jumps off, isTouching won't be set again and wasTouching will also be false. Next time the player jumps on the two will differ and your block will execute once again. But this will trigger for anything the player touches, so you'd need to check specifically for the box.

    If you want your box to receive the messages instead of the player then you have no option but to add another collider to the player. Make sure you add it wihtout replacing the CharacterController. The CharacterController won't send any messages to anything but the object it is attached to.