- Home /
2D Game RigidBody Vs. Character Controller
Hi Unity,
Im working on a 2d game, and came down to the point where i needed to detect collisions by direction, and that lead me to Character Controllers, after playing around a bit i discovered its not practical for my game for several reasons, especially since i rely on forces a lot and that requires a rigidbody, tried using both with disappointing results as they were not meant to be used simultaneously.
My questions is, is there anyway around this issue, such that i can detect collisions without relying in a CHaracter Controller?
Any feedback, suggestions, and obviously solutions are welcome.
Thnx in advance
Cheers :)
Answer by tzamora · Mar 19, 2013 at 08:11 PM
For 2d I usually prefer character controllers, because they allow me to do a more manually movement that is more natural for 2d games (like mario or castlevania).
Rigid body is a nice solution but you depend on the physx engine and your tweeks.
If you have a problem with collisions or collision detection, maybe you are not setting the triggers well, or maybe you need some helper methods to work with your character controller
look at this c# extension that returns if the character controller is touching a wall for example
public static bool isTouchingWall (this CharacterController characterController)
{
return (characterController.collisionFlags & CollisionFlags.Sides) != 0;
}
just like this you could find more helper methods for the character controllers.
Also remember that when using colliders the OnTriggerEnter will only dispath if one collider has its trigger false and the other collider has its trigger in true. (it doesnt matter if is rigid body or character controller)
player.collider | other.collider | OnTriggerEnter will dispacth
trigger=true | trigger=false | yes
trigger=false | trigger=false | no
trigger=true | trigger=true | no
trigger=false | trigger=true | yes
Thats all.
Thnx, for all your feedback guys been really helpful.
That thread was some amazing piece of work.
And after some thought, a character controller was an obviously better solution.
Thnx again guys
Cheers :)
Character controller should be used as long as you are trying stuff like mario. If you want to go for gravity things like walking on the walls and upside down on the ceiling then CC won't do as you cannot rotate the capsule (at least I have not managed) and then rigidbody comes to the rescue as you can do anything with it.
Answer by xt-xylophone · Mar 19, 2013 at 07:42 PM
Rigidbodies have lots of ways to detect collisions thankfully.
Add a script to the rigidbody you want collisions for and use one of these 3 methods:
OnCollisionEnter: OnCollisionEnter is called when this collider/rigidbody has begun touching another rigidbody/collider.
OnCollisionExit: OnCollisionEnter is called when this collider/rigidbody has stopped touching another rigidbody/collider.
OnCollisionStay: OnCollisionStay is called once per frame for every collider/rigidbody that is touching rigidbody/collider.
Each of them will create a Collision object which has the information:
relativeVelocity: The relative linear velocity of the two colliding objects (Read Only).
rigidbody: The Rigidbody we hit (Read Only). This is null if the object we hit is a collider with no rigidbody attached.
collider: The Collider we hit (Read Only).
transform: The Transform of the object we hit (Read Only).
gameObject: The GameObject whose collider we are colliding with. (Read Only).
contacts: The contact points generated by the physics engine.
Hopefully you can use that information to handle any kind of complex collision detection you need :)
check out:
http://docs.unity3d.com/Documentation/ScriptReference/Rigidbody.html http://docs.unity3d.com/Documentation/ScriptReference/Collision.html
I guess my main question was misleading.
$$anonymous$$y concern is the use of the collision flags such as above and below that are a part of the Character Controller, thats what im after really.
Is there anyway to achieve that without a Character Controller?
Thnx
I suppose with the collision info you "could" calculate all the things you need akin to the controller, otherwise ^ boom! tzamora seems to have it
Your answer
Follow this Question
Related Questions
2D rigidbody movement & walls 2 Answers
A node in a childnode? 1 Answer
Need help with screen resolution and character size 1 Answer
Glitching between box colliders. One is kinematic... Help! 1 Answer
A Question about sprites 3 Answers