Magical Rigidbody/CharacterController free character...controller - Unity
Hi
So it seems a lot of people have spoken against the evils of using a rigidbody in Unity so I have been using a CharacterController which is about 99% of what I need. This was fine until I was asked to look into that 1% and see if anyone wouldn't mind sharing how they handle character's and collisions.
The player character we're using is a squarish shape so using the CharacterController's Capsule collider is a bit of a nuisance. I did try use a Box Collider but that resulted in 3 problems:
1 - Using RayCast means I would need to emit a ray from each corner when moving he character which means at least 4 rays but at most 12 per frame. Even so if an object could still pass through the centre of the character undetected.
2 - The OnCollision events only get triggered if a rigid body is attached to the object (which kinda defeats the purpose of all this effort)
3 - Physics.CheckSphere is not Physics.CheckCube... and Physics.CheckCube doesn't exist (nor does Physics.CheckSphere return a list of the colliders within the sphere for additional checking with collider.bounds.Intersects).
If you can please let me know what you use for your player's collisions (or if you do just use character controller too just say so too).
P.S I have attached a drawing of our punk rock horse character code named bob in the collision cube I'd like to use. (It's actually meant to be a zebra but now you see why the powers that be have me doing the programming)
So it seems a lot of people have spoken against the evils of using a rigidbody in Unity so I have been using a CharacterController which is about 99% of what I need. This was fine until I was asked to look into that 1% and see if anyone wouldn't mind sharing how they handle character's and collisions.
The player character we're using is a squarish shape so using the CharacterController's Capsule collider is a bit of a nuisance. I did try use a Box Collider but that resulted in 3 problems:
1 - Using RayCast means I would need to emit a ray from each corner when moving he character which means at least 4 rays but at most 12 per frame. Even so if an object could still pass through the centre of the character undetected.
2 - The OnCollision events only get triggered if a rigid body is attached to the object (which kinda defeats the purpose of all this effort)
3 - Physics.CheckSphere is not Physics.CheckCube... and Physics.CheckCube doesn't exist (nor does Physics.CheckSphere return a list of the colliders within the sphere for additional checking with collider.bounds.Intersects).
If you can please let me know what you use for your player's collisions (or if you do just use character controller too just say so too).
P.S I have attached a drawing of our punk rock horse character code named bob in the collision cube I'd like to use. (It's actually meant to be a zebra but now you see why the powers that be have me doing the programming)


Untitled-1.png
250 x 226 - 24K
Comments
Or is the problem that you don't have logic that can do stuff like test for specific collisions between objects?
I am using rigidbody a great deal in one of my current projects and have had good results with the character controllers previously *albeit not with such a cool geometric zebra as you have*. What problems are you referring to that you need to avoid. You might be skipping the very tool you need. Have you used a rigid body and found specific problems? If so perhaps we could tackle those instead.
Alternatively so you need a cube or could me make your little guy a sphere or perhaps a capsule (2 capsules one aft and one fore if you use controller) again.
Attach a RigidBody to your object and set Is Kinematic to true on that RigidBody. With Is Kinematic set to true, the RigidBody won't invoke the cost of the physics, but it will cause Triggers to fire. Additionally, while the Kinematic object won't receive collisions, other Rigidbodies will receive collision events with it. So, a bullet or whatever will detect that it's collided with your player's kinematic Rigidbody. It will then send a message to your player saying "I hit you bro, for X damage."
See here :
http://docs.unity3d.com/Documentation/Components/class-Rigidbody.html
Does that help, or have you already tried that?
About your horse shape, can you tilt the capsule collider? May be a better fit?
@tbulford : manually moving non-kinematic rigidbodies creates a significant hit in performance, as far as I know.
There is also Bounds.Intersect which you can look into for AABB checks, but I've never used it so I can't advise on its usefullness.
(not asking about how to check other moving objects against it?, which is a simpler problem I think)
I go with number 1. If your terrain is so irregular that raycasts might miss small objects, then I might try spherecasts.
The last FPS I did seemed to work okay with raycasts, but we had quite low detail terrain. We're doing another FPS for the 7DFPS next week... so maybe I can help better then.
Disclaimer: I'm not too sure that I can in good conscience recommend doing things the way I do... I've never worked with a punk-rock horse before, and my games are silly
And I'm not actually too sure how the Unity character controller works...
Rigidbodies I find to be a headache in anything that isn't cosmetic or only interactive in physics simulation related ways.
But I don't know if those are the evils that @mushu is referring to.
Disclaimer: I have no clue how that stuff works, it's pretty much some voodoo magic I believe, well the rays certainly look like pins
@dislekcia I'm still relatively new to game dev and having the actual moving objects detect collisions seemed like it would take more processing power since they only need to react with the character. But the approach you mentioned does make sense so I'll try that. Thanks.
@tbulford The evils I talk about is more along the lines of advice that I got from some guys at the game dev meetups. They mentioned a few of the things that @BlackShipsFilltheSky covered.
@raithza Being new to unity I didn't know that raycasts were inexpensive.
I have read that using Spherecast's can be though.
Sorry for replying so late. I've been locked out of the forums (the password reset didn't seem to wanna work)