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 DubstepDragon · Feb 01, 2013 at 10:30 PM · collisioninstantiatetriggerdoorfire

Having a door open/disappear when collided with a specific particle...

For those who do not know, I am working on a game that involves the character changing his state (fire, water, air, etc...) via standing on a trigger. He then has the power to shoot fire/water/air/other when in the specific mode. I have that basic functionality in place. Now, however, I was looking forward to something more advanced - Involving the player having to shoot a fireball at a door in order for it to open, and it would not work with other powers. I have tried collision and triggers, but have failed miserably, and consulted this helpful community instead.

If any of you can guide me/help me out with the script, I would greatly appreciate it. Preferably I would like it in C#.

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

Answer by aldonaletto · Feb 01, 2013 at 11:20 PM

It depends on how you're implementing the fireball. A simple "fireball recipe": create an empty object ("Fireball") and add to it a sphere collider, the particle effect, an AudioSource and a kinematic rigidbody (set Is Kinematic in the Inspector), then set the collider's Is Trigger field in the Inspector to make it a trigger. Tag this object as "Fireball" and add the script below to it:

 using UnityEngine;
 using System.Collections;
  
 public class Fireball: MonoBehaviour {
 
     public float speed = 8.0f;
     public AudioClip hitSound; // add a hit sound, if desired
 
     void Start(){
         Destroy(gameObject, 6); // suicide in 6 seconds
     }
 
     void Update(){
         // move the fireball forward at speed units/second
         transform.Translate(0, 0, speed * Time.deltaTime);
     }

     void OnTriggerEnter(){ // hit something:
         // play the hit sound, if any...
         if (hitSound) AudioSource.PlayClipAtPoint(hitSound, transform.position);
         Destroy(other.gameObject); // and die
     }
 }

If you set the AudioSource Clip property to some sound, the fireball will play it when instantiated (Play On Awake is set by default). You can also add a hit sound to the field Hit Sound in the Inspector.
Finally, drag this object to the Project view to make it a prefab.
Now, the door: add a script like below to the door prefab, so that it will destroy itself when some trigger tagged "Fireball" hits it:

 using UnityEngine;
 using System.Collections;
  
 public class FireballDoor: MonoBehaviour {
 
     void OnTriggerEnter(Collider other){
         if (other.tag == "Fireball"){
             Destroy(gameObject);
         }
     }
 }

You may of course elaborate the OnTriggerEnter function so that it makes more interesting things when hit - like sinking in the ground with a convenient sound, for instance:

 ...
 public class FireballDoor: MonoBehaviour {

     public float speed = 2.5f; // sinking speed in units/second
     public float doorHeight = 1.8f; // the door height
 
     IEnumerator OnTriggerEnter(Collider other){ // this is a coroutine now!
         if (other.tag == "Fireball"){ // if hit by a fireball...
             float endY = transform.position.y - doorHeight;
             if (audio) audio.Play(); // if some audio effect, play it
             while (transform.position.y > endY){ // repeat until door completely sunk 
                 transform.position -= new Vector3(0, speed * Time.deltaTime, 0);
                 yield return null;
             }
         }
     }

You should add an AudioSource to the door, and set its Clip property to some suitable audio clip in order to have a nice "sinking" sound.

Comment
Add comment · Show 4 · 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 DubstepDragon · Feb 02, 2013 at 06:02 PM 0
Share

Thank you very much! This works very nicely together. There's one thing I wanted to know more about - When I applied this to different doors (such as water, air, earth, etc...) it did not function correctly. Why would that be the case?

avatar image aldonaletto · Feb 02, 2013 at 06:33 PM 0
Share

Do you mean shooting a fireball at a non fireball door? What doesn't work correctly, and what's it doing?

avatar image DubstepDragon · Feb 02, 2013 at 10:35 PM 0
Share

Shooting a fireball at a non-fireball door does nothing(that is exactly what I want it to do). But I have duplicated the script and edited it to recognize the other tags (I have all the tags in place) such as "Waterball" and "Airball", but it does nothing. I wonder why this does not function correctly, but I remain un-knowing until I seek the wisdom of your graymatter...

avatar image aldonaletto · Feb 03, 2013 at 04:14 AM 0
Share

You could use exactly the same first script for WaterBall or AirBall - only the prefabs should be tagged as "Waterball" and "Airball". The door script could be almost the same as above - just compare to the appropriate tag. If this isn't working, start adding debug lines to your code - add this line to the door script, for instance:

 void OnTriggerEnter(Collider other){
     Debug.Log(other.tag); // add this line
     if (other.tag == ...

This debug line prints the tag of the object that hit the door - if the tag is ok, place another debug line inside the if, and so on.

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

10 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Prevent instantiating on collider 1 Answer

Making a turret fire another arrow if an enemy survives the hit 3 Answers

Lightswitch help? 3 Answers

Putting an object somewhere to destroy something else 1 Answer

Affect only one clone from multiple clones? 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