1
\$\begingroup\$

I have Cube with a sword like object as its child object. But when the cube moves in the scene the sword goes into the other objects in the scene. The sword have a BoxCollider attached to it but the scene contains objects that are stationary and don't have a Rigidbody attached as I don't want them to be controlled by physics, same for the sword. If I attach Rigidbody to it then it will be displaced from its main position on collision.

I tried to render only the Cube including the sword with one camera having higher depth value then the main camera in the scene. If my game was in first person then this works fine but In 3rd person when object get behind other objects in the scene we can still see the object.

How can I prevent the sword from going into other object without being controlled by physics?

Image: the image of sword going into object

\$\endgroup\$
1
  • \$\begingroup\$ Only one of the two items need to contain a rigidbody to utilize the collision. You can also have equal and opposite reactions to negate the physics and have things not move. \$\endgroup\$
    – n_plum
    Commented Aug 2, 2017 at 16:22

1 Answer 1

1
\$\begingroup\$

Put the colliders on the the objects in your scene that you don't want the sword to pass through and leave off the rigidBody. You don't have to have a rigidBody on those objects for the collision detection to make the object have a physical presence in the scene, and if it is an object that does need a rigidBody, you can mark the body as isKinematic in the editor. That means it will not accept an in-coming force (therefore it wont get moved).

You may want to read up on the Unity Docs on RigidBody and Colliders because depending on how you set this up will change how you script things like OnCollision()

\$\endgroup\$
3
  • 1
    \$\begingroup\$ If you want to detect collision then you will at least need a Rigidbody with isKinematic false in one of the object and colliders on both of them.. \$\endgroup\$
    – coder_86
    Commented Aug 2, 2017 at 16:31
  • \$\begingroup\$ ^ yes, this is what I mean by read up on the docs. There are quite a few gotchas that could trip you up with your collision detection scripts. \$\endgroup\$ Commented Aug 2, 2017 at 16:34
  • \$\begingroup\$ The simplest way is to have the script on the enemy detect the collision from an attack, but it often makes more sense to put it on the weapon. That requires some careful scripting to turn things on and off. I usually control that directly in the animation. \$\endgroup\$ Commented Aug 2, 2017 at 16:44

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .