Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 NixyGames · Apr 02, 2017 at 10:09 AM · collisiongameobjectphysicsshooting

How to collide with everything but trigger script whenever certain object hits

I have a cannon and a wall, i have a setup cannon which shoots canonball at a wall and i have a simple script that works by deleting old wall and replacing it with new fractured one. But OnCollisionEnter doesn't work very good, because wall(or cubes) very often collides with ground or each other i tried ignoring ground but then cube falls through ground. Is there any other way? Maybe to still collide with everything but execute script whenever cannonball hits wall, but i'm not sure how to do that... PS: I tried with tags or layers but that didn't work either.

 public class Destructible : MonoBehaviour {
     RaycastHit hit;
     public GameObject destroyedVersion;
     void Start() {
     }
     void OnCollisionEnter(Collision collider)
     {
         if (hit.collider.gameObject.tag == "cannonball") {
             Instantiate (destroyedVersion, transform.position, transform.rotation);
             Destroy (gameObject);
         }
 
     }
 
 }

 

Comment
Add comment · Show 2
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 RobAnthem · Apr 02, 2017 at 11:27 AM 0
Share

Have you considered using a trigger collider ins$$anonymous$$d?

avatar image NixyGames RobAnthem · Apr 02, 2017 at 05:31 PM 0
Share

@RobAnthem Well, i fixed it partially, now it doesn't collide with ground but collides with player, basically what i wanted is to only collide with my cannonball, i'm now using OnTriggerEnter, which is the same but still not great because i'm failing to see a way to avoid executing my destructible script when cubes hit each other. I can't really explain what i want.. Basically i wan't my cube to execute destructible script when it's hit by something(in this way its trigger collider)Cube on top should fall as well(by adding mass so it's not flying away when bottom one explodes) player can go trough cube, but when does he doesn't execute destructible script (only cannonball does) and if a cube is falling down so when it hits the floor, script is executed. And so on.. i really can't find a way to do this.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by toddisarockstar · Apr 02, 2017 at 05:21 PM

what is the raycast variable for?

if you want a collision you dont need a raycast variable. it should just look like this:

  public class Destructible : MonoBehaviour {
     
      public GameObject destroyedVersion;
      void Start() {
      }
      void OnCollisionEnter(Collision collider)
      {
          if (collider.gameObject.tag == "cannonball") {
              Instantiate (destroyedVersion, transform.position, transform.rotation);
              Destroy (gameObject);
          }
  
      }
  
  }

Comment
Add comment · Show 3 · 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 NixyGames · Apr 02, 2017 at 05:37 PM 0
Share

@toddisarockstar I was trying something with raycast, but then i forgot to delete it.. And that in void start i forgot to delete, i was trying a couple things, but they didn't work.

avatar image toddisarockstar NixyGames · Apr 02, 2017 at 09:17 PM 0
Share

the raycast varible was also in the collision function. if what i wrote isnt working try putting a print statement in the collision function to see if thats the problem.

did you put rigidbodys and colliders on everything?

avatar image NixyGames toddisarockstar · Apr 02, 2017 at 09:52 PM 0
Share

@toddisarockstar I already tried that what you wrote, as i said in comment with Rob Anthem i can't find a way how to do this. Yes i did put rigidbodys on everything, i added another box collider on my cube(i shifted it so i doesn't touch ground) i changed my script a bit and now it works, but i still doesn't work now a player can trigger or other cube( so now cubes can't be stacked) or anything else... This is my script now:

 public class Destructible : $$anonymous$$onoBehaviour {
     public GameObject destroyedVersion;
     void Start() { 
     }
     void OnTriggerEnter()
     {
             Instantiate (destroyedVersion, transform.position, transform.rotation);
             Destroy (gameObject);
         
     }
 
 }

Very simple as you can see.

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

How to detect collision with imported object? 0 Answers

Button Box Door Collision 2 Answers

How to achieve this basic physics effect? Detect when to change layers 2 Answers

How to delay time reset on collision? 2 Answers

Exploding two gameobjects from the center of an collision 1 Answer


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