- Home /
The question is answered, right answer was accepted
c# trigger collider makes character controller misbehave.
Hiya,
This is a bit difficult to explain so im sorry if its a bit long.
Im making an RTS. So, I have unit that moves with a character controller. I also got a tree in my scene (basically a cube scaled only on its y axis). when I select my unit and make it move anywhere, it works fine.. Also when I click on the tree to make the unit move there it works fine. when the unit nears the tree the unit will do a "gather wood" animation wich works fine. Now I've placed a trigger collider (sphere collider set as trigger) on the tree to detect if there is anyone near it and does the gather wood animation, give wood every second or so.
however, when I click on the tree to make the unit move there instead of moving directly towards the tree, the unit moves between the tree and the inside edge of the sphere collider and spins uncontrollably. the unit always favors a certain spot when the collider is on. when I deactivate or remove the collider and let the unit move towards the tree again it works normally.
I tried different colliders and came to the conclusion that if I either remove or make the collider just to small for the unit to enter, its the same regardless of whether its a box collider, a capsule collider or whatever. if its too big, it will spin uncontrollably inside that collider on the exact same spot as last time which is somewhere between the positive z axis of the tree and the edge of the collider.
Is the collider messing with the character controller collider or something? I know its not a script issue but if it can be fixed through scripting then I prefer c#. Ohw and yes, the collider is set as trigger.
If you need more information, please feel free to ask.
Answer by N1warhead · Oct 04, 2014 at 11:38 AM
I've figured out a long time ago when collider don't work, I settle for trying to do a Raycast.
Try doing a raycast. Have the tree or player send out a raycast to the tag of the tree or player, if it's within a certain distance x from the tree or x from the player, then play your animation and do what you need to do with your Gather wood.
So pretty much lets say this is on your player.
if (player) > 5 units from tree > Raycast ticks > Whatever is in your If statement on your Raycast > Activates. If your more than 5 units > The IF statement is voided out unless you go back within > 5 units.
Hope that makes sense?
NOTE - THAT WAS NOT A CODE. AND ALSO LOOK INTO SPHERECAST.
been actually looking a bit into spherecasting (saw it somewhere when I watched the "related questions" on the right side) before you posted your answer but I cant seem to get the spherecast expand like a spherecollider.
I can only make it expand on 1 axis e.g. positive Z axis with transform.forward.
I tried vector3.one but doesn't seem to work and expanding radius doesn't either.
Am I forced to put 8 spherecasts on (like 4 sides + 4 corners of the tree) and play around with Physics.OverlapSphere?
wouldn't that be taxing on performance when I got loads of trees in my scene?
Never$$anonymous$$d, im an idiot.. had that spherecasting inside a method and the method on void update inside an if statement that always returned false.. :P
Thnx for the help and confir$$anonymous$$g that spherecasting is what I needed.
By the way, do you happen to know why trigger colliders and character controllers cant work well together? (case I still don't understand why it shouldn't work..)
Glad you got it working buddy :).
To be honest, I've never had that problem exactly like you have, my stuff either just didn't work or it did lol. If I couldn't get somethign to work I just tried a different path, I hate ray casts, they are complicated. Like I made one the other day and ins$$anonymous$$d of going down. Oh it went down, but when I jumped it would shoot forward, like really? Lol.
But I always end up making my own custom controllers. $$anonymous$$uch easier.
Follow this Question
Related Questions
Destory/Collect object to open door 1 Answer
How do I detect when two CharacterControllers collide? 0 Answers
Trigger help! 0 Answers
OnCollisionEnter not working 1 Answer
Adding force relative to the distance from an object 1 Answer