Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by omgdave · Jul 02, 2015 at 03:50 PM · collisionrigidbodylayerignoreselective

How to make a rigidbody bullet move through a platform?

my character is shooting a gun. I want the bullets to shoot up through platforms but collide when moving down. They are arcing, mortar type bullets, and I would like to tie their platform collision to their velocity in y.

I can't do Physics2D.IgnoreCollision(c1, c2) because it will still impact at least once. I can't set it to kinematic, because it still needs to collider with enemies. I can't do IgnoreLayerCollision(layer1, layer2) because then bullets moving down will also be affected. I could have 2 layers, one for bullets moving up and 1 for bullets moving down. That is all I can think of.

I would like to be able to just set a layermask value on the prefab. Is there any way to make specific colliders/gameobjects ignore specific layers?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by jfjordanfarr · Jul 03, 2015 at 07:28 AM

Okay, this might not be a 100% perfect answer, but how about this?

Given that bullets tend to continue moving in the same direction with essentially constant velocity, how about you go into the bullet script and start checking contact point "normals" (see http://docs.unity3d.com/ScriptReference/ContactPoint2D-normal.html)? The "normal" is perpendicular to the point of contact, and helps you figure out the orientation of two objects that end up in a collision. If there is a particular y value on a contact point's normal (for collisions above the object in question I believe that value is -1), you could set the rigidbody's velocity to be exactly the same as it was in the last frame (or last FixedUpdate), hopefully enabling you to pass through ceilings.

Here's one possible code block for how I'd go about this:

 void OnCollisionEnter2D(Collision2D col)
 {
      if (col.contacts[0].y == -1)
      {
           RB.velocity = previousVelocity;
      }
 }

You'd need to rig up the bullet's rigidbody as RB or change RB to whatever variable you have, and set previousVelocity somewhere like FixedUpdate. Adding gameObject tag or layer checks could also help eliminate bullets passing through enemies if the bullet came from below them.

You could also test this on CollisionStay2D as well if need be, and even check more contact points' y value than just the first contact point.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image omgdave · Jul 20, 2015 at 06:14 AM 0
Share

I have done similar things when not using rigidbodies, where I essentially abandon the 2d physics system and write my own collision.

This time, however, I am trying to stick with it.

That said, doing your described method, you will not be able to pass the bullet object through the platform. If OnCollisionEnter is being called, then one of those is a non-kinematic rigidbody. If the bullet is non-kinematic (which it should be to arc and bounce it), it will not be able to pass through the platform (kinematic or otherwise). Setting the velocity will not help.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Ignore Collision Layer Ignores also other layers 1 Answer

Make a rigidbody collide with this rigidbody but not that one? 1 Answer

What is an effective way of colliding but passing through another surface? 2 Answers

OnCollisionEnter doesn't work if enemies and allies have rigidbody ? 1 Answer

Rigidbody: how to get collisions for the current frame ? 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges