2
\$\begingroup\$

I'm creating something along the lines of Super Smash Brothers. There's a problem with the collision between the two players and projectiles.

We have rigid body on the characters and projectiles and every time they collide they push one another back, I know that is how it operates but is there a way to stop that from happening?

\$\endgroup\$
3
  • \$\begingroup\$ How are you moving the game objects? Are you setting their velocity or adding forces to them? \$\endgroup\$
    – fryBender
    Commented Feb 24, 2015 at 15:07
  • \$\begingroup\$ @fryBender we are using transform.position \$\endgroup\$ Commented Feb 24, 2015 at 15:12
  • 1
    \$\begingroup\$ If you set the projectile's Collider to "Trigger" then it will still detect collisions (using OnTriggerEnter instead of OnCollisionEnter), but will not push objects with which it collides. \$\endgroup\$
    – DMGregory
    Commented Feb 25, 2015 at 4:17

3 Answers 3

4
\$\begingroup\$

Set the mass of your projectiles to be very low, and the mass of your characters to be higher until they are no longer affected by the collision of the projectile (mass is a variable on rigidbody component).

The reason your rigidbody's push each other away is because this is what physical objects do in real life and rigidbody simulates this as accurately as possible. You throw a light ball at a heavier ball, both balls react. The heavier ball moves less than the lighter ball. If it was a really heavy ball, or a rock I guess, the really heavy ball/rock wouldn't move noticeably.

This will work if you're expecting your projectile to be destroyed on hit like Mario's fireball. If you want to use a beam or something like some of the final smashes, you'd make the beams "hitbox" (http://en.wikipedia.org/wiki/Hitbox) a trigger that exists for one FixedUpdate frame and deals its damage/applies its forces and then goes away the next frame.

Also, when moving rigidbody's, do so via rigidbody.AddForce or add to its velocity yourself, as setting the transforms bypass the physics simulation and can easily result in you moving your objects into one another and cause them to become stuck. AddForce is more realistic at times, and just adding to the velocity can be easier to use at sometimes.

This is easy for a platformer, example in FixedUpdate:

if(rigidbody.velocity.x < maxMoveSpeed) { //and pressing right
    //apply force right
}
if(rigidbody.velocity.x > -maxMoveSpeed) { //and pressing left
    //apply force left
}
\$\endgroup\$
1
\$\begingroup\$

you can create two GameObjects one with a collider and one with a Rigidbody. link the position of the one with a collider with that of the one with a Rigidbody. Now both can move with physics without interacting with other gameObjects. Use the one with a collider for collision detection and such.

\$\endgroup\$
3
  • \$\begingroup\$ I was thinking of doing that in OnCollisionEnter as in doing what you want the bullet to do but I tested it and it seems for a few frames they can still interact with each other so I'm working on it \$\endgroup\$
    – DarQ
    Commented Jan 13, 2021 at 14:34
  • \$\begingroup\$ It should work now. \$\endgroup\$
    – DarQ
    Commented Jan 13, 2021 at 16:45
  • \$\begingroup\$ Thanks for updating your answer :) \$\endgroup\$
    – Vaillancourt
    Commented Jan 13, 2021 at 16:48
0
\$\begingroup\$

In your comment you mentioned that you are setting the game objects positions instead of relying on forces or velocity. If I had to take a guess, the problem you are facing is having two rigidbodies colliding together, which in turn is affecting other rigid bodies. You have two options in this case

  1. Remove the rigidbodies altogether. If you are not using velocity or forces to move your bodies, why attach a rigid body?

  2. Set your rigidbodies to kinematic. This way you'll leave your options open in case you do decide to have objects in your scene that can be affected by physics

Further Information on Kinematic Rigidbodies

To enable kinematics on a rigidbody all you have to do is click on the checkbox labelled "isKinematic". Kinematic rigidbodies are not affected by forces, gravity or collisions. They are explicitly set by setting the position and the rotation of the game object, however they can interact with other non-kinematic bodies.

\$\endgroup\$
1
  • 1
    \$\begingroup\$ reddit.com/r/Unity3D/comments/1d3ma9/… "Objects without a rigid body are considered motionless by Unity, and if they have colliders they get sorted into a high performance scene graph, which greatly increases the physics performance. However, every time you move one of these objects, the graph has to be recalculated, This is slow, and should be avoided if you can help it. Having to rebuild the graph every frame will cripple your performance." \$\endgroup\$ Commented Feb 25, 2015 at 5:57

You must log in to answer this question.

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