Apologies ahead of time: I’m still new to programming, although I’m not here to have anyone do my work for me.
I’ve written a simple pick up and throw script using the 1st person character controller that comes in the standard assets and a cube with a rigidbody attached. The player Game Object has a child Game Object that has a box collider on it with Is Trigger set to true.
I’m trying to disallow the player from picking up the cube if they are standing on it, which tends to cause a physics breakdown and makes the player shoot into the sky.
I’ve attempted to have the player’s trigger object have a script with OnTriggerEnter (just with a console print saying “colliding!” for now). I’ve tried OnColliderEnter on the box and the trigger. I’ve tried everything I can think of, but I CANNOT figure out why no console print is appearing. I’m happy to post snippets of code here or in pastebin, but I’m sort of out of ideas.
I’m happy to upload my project for anyone that can help. I’ve got some multiplayer stuff happening too that might be causing this? I don’t see why though.
Have you tried making an empty project and testing this in it? All you should need the script and trigger on is your collider contained within the player object. It sounds like something else is interfering with it.
Start a new project and set it up exactly as you described in your first post with the following on the collider that’s within your player object and it should work. If it does you should start looking for conflicts in your main project.
function OnTriggerEnter () {
Debug.Log ("Hello!");
}
I had that same idea actually. I set up a new project. Imported the stock First Person controller, added an additional child game object to the controller named Trigger. I removed the mesh renderer and mesh filter and added a Box Collider which I turned into a trigger. I wrote a quick script that said:
using UnityEngine;
using System.Collections;
public class Collision : MonoBehaviour {
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
void OnTriggerEnter ()
{
Debug.Log("Colliding!");
}
}
I attached the code to the Trigger Game Object.
Then I added a cube Game Object to the scene with a rigidbody. I moved my character up to the box until the Trigger’s box collider trigger was intersecting with the cube’s rigidbody, but I got no debug print
Hmm, technically you should get the debug print before you ever even touch the rigidbody because that box collider should be colliding with your player object and triggering it since there’s no code telling it to only trigger on the cube.
Leaving that tidbit aside, it could be because you’re using a box collider and perhaps have it fairly small so only the corners can trigger. You might want to try a capsule collider so it has an even amount of collider space all around the player object. Also try the following and put a tag on your rigidbody cube so it only triggers when the cube is hit:
(Also sorry I’m posting JS when I see you’re using C#, I don’t know C# syntax enough to be sure I’m not posting up code with errors.)
function OnTriggerEnter (hit : Collider) {
if (hit.gameObject.tag == "Cube") {
Debug.Log (hit.gameObject);
}
}
If not, and you have maybe just written that function here instead of a direct copy from your project, make sure at least 1 of the colliding bodies has a non-kinematic rigidbody attached.
using UnityEngine;
using System.Collections;
public class Collision : MonoBehaviour {
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
void OnTriggerEnter (Collider other)
{
Debug.Log("Colliding!");
}
}
However, with this script attached to the Trigger Game Object OR the cube with the rigidbody, I’m still getting no debug prints. What am I missing?
Maybe the rigidbody is sleeping?
Try adding this script to the rb object to keep it awake:
Also might want to change your class name to something else, Collision is used by unity…(not sure if that really matters but…)
public class Collision : MonoBehaviour {
Its also possible that they are in different layers, which are set not to collide…
Is it possible to wake the rigidbody up in the collider script attached to the player object? I’d rather not have every rigidbody in the scene awake on every frame if it isn’t necessary
Still no luck. isTrigger on the player’s Trigger object is definitely set to true, and I’ve got a script running every frame on the cube to wake up its rigidbody. I even tried just having the box drop onto the player’s trigger to ensure that the rigidbody would be awake, but it’s still not triggering the console print.
I feel like I’m just missing one important piece of information, the documentation makes this seem so simple!
Sorry, didn’t notice that mgear had already suggested that. So if both, filename and class name has been changed to something else, you might wanna upload screenshots of your setup? This can simply show a new scene with the script attached to the object with a rigidbody and another object being the trigger so we can have a look at it.
I get this message:
Colliding!
UnityEngine.Debug:Log(Object)
CollisionTest:OnTriggerEnter(Collider) (at Assets/CollisionTest.cs:18 )
(remove the other camera so you’ll see the messages better…otherwise it shows that “There are 2 audio listeners in the scene. Please ensure there is always exactly one audio listener in the scene.” million times…)
Also it might collide the derp character collider instead…