- Home /
I'm making a top-down action game. Should I use Rigidbody2D for collision even though I won't be using physics otherwise?
It seems like a waste to make everything into a physics object just to handle collision detection (basically I just don't want things passing though each other and detection for getting hit with player and enemy attacks), but on the other hand, from what I've been reading setting up collision detection without using RigidBody is much more complicated. Any suggestions?
Well, since there is not CharacterController2D (unfortunately), you kind of have to use a RB2D if you don't want to write your own collision detection system (which is possible as well, and there are plenty of resource for it online). I'd suggest looking into using a RB2D and if it works for you without too much hassle then go with it.
@wee_ooh I updated the answer, sorry it took so long. Working on dinner.
Answer by EpsilonQoppa · Jul 09, 2017 at 08:37 PM
So, I totally lied to you.
That being said, this still does not mean you need all of your objects to be physics based items.
The objects you want your character to not be able to pass through only need colliders.
However, you will need your character to have both a rigid body and a collider to trigger the oncollision event.
As to your question in the earlier comments, you will not need to code any type of system to keep your character from passing through objects. This is something colliders do intrinsically.
Having said that, you may want other things that will exist in large numbers, to enact a function when impacting an object without each of these instances undergoing physics updates.
In such cases, you can do something like this:
Transform m_Transform;
SphereCollider m_Collider;
void Start ()
{
m_Transform = GetComponent<Transform>();
m_Collider = GetComponent<SphereCollider>();
}
void Update ()
{
if (Physics.OverlapSphere(m_Transform.position, m_Collider.radius).Length <= 1)
m_Transform.localPosition += (Vector3.forward * Time.deltaTime);
else
Explode();
}
This will allow you to avoid calling fixed update every 0.02 seconds and still have things like projectiles impact targets.
Haha no problem man, thanks for the update. I'll try the code you suggested. $$anonymous$$y objects with colliders attached aren't blocking each other, though, and I can't figure out why. Is there an option I have to enable (or disable)?
Just having a collider is not enough. The object that is moving and should be stopped must have a non-kinematic RigidBody or equivalent (CharacterController, for example) as well. the involved colldiers can't be triggers either.
Your answer
Follow this Question
Related Questions
How do I make Rigidbody2D movement smoother? 0 Answers
Instantiate new gameobjects on collision, keeping velocity, up to a cap 1 Answer
Object passes through collider even when isTrigger is turned off. 2 Answers
2D Sprite drifting down, otherwise passes through walls - how do I fix these issues? 0 Answers
Best way to rotate around a point with a RigidBody 2 Answers