Collisions won't work(Please check text)
Hi guys,
I know this question has been asked like thousands of times, but after I checked pretty much every other question I still couldn't fix the problem. So I thought I should have write here about this. Now before I start my question please excuse me for my bad english, I'm not native.
So here it is:
In my game I have the standard unity FPS controller with a rigidbody fixed to it(since I want to experience the full body awareness). The basic FPS script comes out with a character controller which is if I'm not mistaken an actual collider(I've just enlarged the radius so part of my body and weapons wouldn't clip through things) so it should work on itself. Point being I have a rolling boulder with a sphere collider attached to it(no trigger) and a rigid body with a greater mass than my player. As they make contact the player should change is animation and fall on the ground, but I've managed to make this happen only a couple of times and I've never changed the actual code.
This is the code:
function OnCollisionEnter (other : Collision){
if (other.collider.CompareTag("Hazard")) {
anim.SetTrigger("Die");
}
}
Yes the boulder has the "Hazard" tag. "anim" is my reference to the animator component and so far worked for all the other animations.
What usually happens is that the boulder simply raise above the player head and once he passed through it keeps walking till the end of the map. They definitely make contact though. The only idea I had so far was that the character controller works differently from normal colliders but removing it and putting a capsule collider made the FPS controller unusable(not that surprises me, but it would require changing a lot of code not made by me).
Other issue I have with colliders is: I have a weapon spawning only when I press a button(by default both mesh renderer and capsule collider are disabled). When I spawn the weapon the mesh render is enabled but the capsule collider only activates when I press the "Attack" button. When I press the "Attack" button I can see in the inspector the capsule collider becomes enabled for the duration of the attack animation, then it is disabled again. During this period if the capsule collider of the weapon collides with other colliders something should happen. For example I have this wall and I want to play a sound when the weapon hits it. But the code:
public void OnTriggerEnter(Collider other){
Debug.Log (other.gameObject.name);
if (other.gameObject.tag == "Wall") {
Destroy (other.gameObject);
PlayWallSound ();
}
}
Seems not to be working. I know that in order to actually collide both objects must have a collider not set as trigger and at least one of them must have a rigidbody attached. In my case wall has a box collider with trigger off and my weapon has a capsule collider with trigger off and a rigidbody set to use gravity and to be kinematic.
It would have made more sense if I'd put a rigidbody on the wall instead of the weapon(or both of them) but I couldn't avoid the wall from falling on the ground has I attack. So i decided to put it on the weapon. Is kinematic is set to avoid the weapon to fall on the ground once it spawns in the hands of the character.
So to reiterate both problems seems to be related to colliders and I'm pretty sure character controller is causing that problem. Hope I've explained the better I could.
Please if you require any other part of the code feel free to ask. I'll answer the faster I can.
Hope you guys can help me, I'm pretty much stuck here.
Thanks for reading till here and thanks in advance to all who'll spend their time helping a newbie as me.
Cheers :-)
UPDATE: For all those who might come to this topic with the same problem.
I managed to solve it with a not very polite solution, but at least it now works.
I put two colliders: One which guarantees that objects doesn't clip through them(the physical one) and a litte larger one which "is trigger" and which make everything work when objects make contact.
Hope this will help someone.
See ya ;-)
Answer by Oliver Edge · Jun 18, 2016 at 11:21 AM
I think I managed to solve the boulder problem by using the proper function for character controllers "OnControllerColliderHit".
Still no way to manage the other problem though.
UPDATE: OnControllerColliderHit has the problem in which my character has to move in order to actually calculate the collision(like it is stated in the unity documentation).
I tried to put a normal OnCollisionEnter but as stated in the first post of this topic I still can't make them work.
This means that the wall still doesn't produce any sound when it and that the boulder doesn't kill the character if it stands still.
Hope someone will read and answer though...
Your answer
![](https://koobas.hobune.stream/wayback/20220612083023im_/https://answers.unity.com/themes/thub/images/avi.jpg)