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 /
This question was closed Jul 07, 2017 at 03:10 PM by Connorkenway92 for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by Connorkenway92 · Jul 01, 2017 at 10:48 PM · scripting problemplatformerbullet

Bullet Script Problem

I have my bullet script everything is right but i wanted to add something to make my bullet disapear when it collide with something a wall for exemple here's the script

  using UnityEngine;
  
  /// <summary>
  /// Projectile behavior
  /// </summary>
  public class ShootScript : MonoBehaviour
  {
      // 1 - Designer variables
  
      /// <summary>
      /// Damage inflicted
      /// </summary>
      public int damage = 2;
  
      /// <summary>
      /// Projectile damage player or enemies?
      /// </summary>
      public bool isEnemyShot = false;
  
      void Start()
      {
          // 2 - Limited time to live to avoid any leak
          Destroy (gameObject, 1);
      }// 20sec
          
  }
  
 


i wanted to add something like this

 private void OnCollisionEnter(Collision collision)
          {
              
              if ( collision.gameObject.name == "wall" )
              {
                  Destroy(collision.gameObject);
              }
          }
 



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

2 Replies

  • Sort: 
avatar image
1
Best Answer

Answer by aldonaletto · Jul 01, 2017 at 11:22 PM

Just add the OnCollisionEnter code to this script in order to kill the bullet whenever it collides with anything:

       void OnCollisionEnter (Collision collision) {
               // if bullet collided with anything...
               Destroy (gameObject); // it suicides
       }
Comment
Add comment · 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
1

Answer by Mercbaker · Jul 01, 2017 at 11:34 PM

Setup:

  • Put collider triggers on the objects you want the projectile to hit.

  • Put a collider and ridgidbody on your projectile

  • Add below script to projectile.

       void OnTriggerEnter(Collider col)
              {
              if(col.gameObject.tag == "Enemy")
              {
                // call enemy take damage method and pass projectile damage 
                col.gameObject.GetComponent<enemy>().TakeDamage(projectileDamage);
                Destroy(gameObject);
              }
              if(col.gameObject.tag == "wall")
              {
                Destroy(gameObject);
              }
         }
    
    

Getting the gameObject.tag works better here because you may have multiple enemies. You would want to name them differently. However they are all enemies, so just add a tag to whatever object is an "enemy".

You can also fire off multiple projectiles all with different damage amounts, then call the "TakeDamage" function on the corresponding objects it hits.

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 Connorkenway92 · Jul 07, 2017 at 03:09 PM 0
Share

Thank You man !

Follow this Question

Answers Answers and Comments

105 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to Disable my player controller script, if I have many characters? 0 Answers

Need to restrict "air control" 2D platformer. 1 Answer

how d you change PlatformEffector2D rotationaloffset change as prefabs in android? 0 Answers

Instantiate creating object far off the screen 0 Answers

Need help accessing booleans from other script. 2 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