1
\$\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\$
  • \$\begingroup\$ How are you moving the game objects? Are you setting their velocity or adding forces to them? \$\endgroup\$ – fryBender Feb 24 '15 at 15:07
  • \$\begingroup\$ @fryBender we are using transform.position \$\endgroup\$ – user3727417 Feb 24 '15 at 15:12
  • \$\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 Feb 25 '15 at 4:17
2
\$\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\$
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
    \$\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\$ – Anthony Raimondo Feb 25 '15 at 5:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.