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 Westerlund · Jan 27, 2010 at 02:01 PM · raycastcolliderparticles

Using different paricle emitters depending on the tag of the object raycast hits?

I'm creating a FPS game and using raycast to detect collisions when the player fires a weapon. As it hits something a particle emitter emits a particle looking like a plaster wall puff. However it's the same for when it hits glass, wood and flesh, the raycast is handled in the player weapons script.

I would like it to use different particle emitters for objects with different object tags (eg. Enemy, Water, Glass etc, and of course I have created such particle effects aswell.) I've spent 3 days trying to make the raycast to detect the collisions tag but I'm not very experienced with coding and obviously I get it wrong over and over again. Would really appreciate some help on this, feels as if I've walked into a dead end.

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
5
Best Answer

Answer by duck · Jan 27, 2010 at 02:08 PM

First, make sure you're using a version of Raycast which includes passing results to a RaycastHit object. The first example in the Raycast Documentation doesn't do this, but the second example does.

Next, simply check the 'tag' variable of the collider that was hit, eg:

var hit : RaycastHit;
if (Physics.Raycast (origin, direction, hit, distance)) {
    if (hit.collider.tag == "Glass") {
        // ...use glass particles...
    }
}

If you're creating a number of different types of surface, you'll end up with lots of 'if' statements in there though, or a switch statement, neither of which are really desirable because you'll end up with hard-coded values which need modifying if you add or change your particles or surfaces. A neater way of setting this up would be to have the code itself check whether there's a suitable emitter for whichever surface was hit:

Create a public ParticleEmitter array variable, which you can then populate in the editor, like this:

var surfaceHitEmitters : ParticleEmitter[];

  • In the Editor: Assign the correct length to the array

  • Drag each particle system type into a slot in the array

  • Assign your tags ("Water", "Glass", "Wood" etc) to the emitter objects too

  • You can then check at runtime whether there's a suitable emitter for the type of surface hit, without much fancy coding at all:

    var hit : RaycastHit;
    if (Physics.Raycast (origin, direction, hit, distance)) {
        for (var emitter : ParticleEmitter in surfaceHitEmitters) {
            if (emitter.tag == hit.collider.tag) {
                emitter.Emit();
                break;
            }
        }
    }
    

    The above code scans through the emitters in the array, and calls Emit() on one if it finds that it has a tag that matches the type of surface that was hit (this is why you need to tag the emitters as well as the surfaces in your scene).

    You could probably optimise this by making a lookup table using a Hashtable or some other key-value lookup system. You could also remove the need to assign tags to everything altogether by picking the appropriate emitter based on the Material of the object hit.

    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 Westerlund · Jan 27, 2010 at 02:44 PM 0
    Share

    :) thanks alot, and thank you for a really quick answer!

    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

    No one has followed this question yet.

    Related Questions

    World particles collider scriptable? 1 Answer

    OnCollisionEnter-issue 1 Answer

    Raycast Destroy(hit.collider.gameObject); (Still need help) 1 Answer

    Raycast 2D with origin inside the collider 1 Answer

    Keep particles to defined area, destroy on collision 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