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 Agent_Cooper · May 12, 2015 at 09:50 AM · collisiondestroybullet

destroy ''fireball'' on collision

So i made this code using several codes found in unity questions, the script is attached to a billboard enemy (doom style).... this is the script the bullets seem like soap bubbles and they don't destroy on collisions

using UnityEngine; using System.Collections;

public class S_skullTower : MonoBehaviour {

  public Transform target;
  public float turretSpeed;
  public float fireRate;
  public float fireBallHeight;
  public GameObject fireBall;
  public float range;
  float distance;
  private float _lastShotTime = float.MinValue;
  // Use this for initialization
  void Start () 
  {
      
  }
  
  // Update is called once per frame
  void Update () 
  {
      //Rotate turret to look at player.
      Vector3 relativePos = target.position - transform.position;
      Quaternion rotation = Quaternion.LookRotation(relativePos); 
      rotation.x=0;
      rotation.z=0;
      transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * turretSpeed);
      
      //Fire at player when in range.
      distance = Vector3.Distance(transform.position,target.position);
      
      if (distance < range && Time.time > _lastShotTime + (3.0f / fireRate)){
          _lastShotTime = Time.time;
          launchFireBall();
      }    
  }
  
  void launchFireBall()
  {
      Vector3 position = new Vector3(transform.position.x, transform.position.y + fireBallHeight, transform.position.z);
      Instantiate(fireBall, position, transform.rotation);
      Rigidbody rb = fireBall.GetComponent<Rigidbody> ();
      rb.velocity = transform.forward * 40;
  }
  
  void  OnTriggerEnter(Collider fireball)
  {
      if (fireBall.gameObject.tag == "Player") 
      {
          DestroyObject(fireBall.gameObject);
          Debug.Log("Bullet was destroyed");
      }
  }
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

· Add your reply
  • Sort: 
avatar image
0

Answer by sas_88 · May 12, 2015 at 11:05 AM

In launchFireBall() u have Instantiated fireBall without assigning to any gameobject.

In OnTriggerEnter(Collider fireball)

check fireball.gameObect.name before checking the fireBall.gameObject.tag

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 Agent_Cooper · May 12, 2015 at 12:29 PM 0
Share

could you make an example with code? anyway, thanks for your answer

avatar image
0

Answer by Eudaimonium · May 12, 2015 at 01:04 PM

You have a logical error, your OnCollisionEnter function should not be in this script that is attached to the enemy.

If this script is attached to the enemy, the OnCollisionEnter will trigger whenever something hits the enemy, and you are checking if (player has hit enemy), then destroy colliding object (which is a player!).

Instead,

  • Remove the OnCollisionEnter function from this script

  • Make a new script and attach it to Fireball object (which will be instantiated when firing)

  • Make a new OnCollisionEnter function in that script and use this:

     void  OnTriggerEnter(Collider other)
       {
           //If the fireball has collided with Player:
           if (other.gameObject.tag == "Player") 
           {
               //Write debug message first:
               Debug.Log("Fireball was destroyed");
     
               //Then, destroy the current instance of gameObject:
               Destroy(gameObject);
           }
       }
    

That will make enemies fireballs disappear when they hit the enemy. (Alternative - maybe make it disappear when they hit anything?)

You will also want to make a similar script for whichever object your Player can fire at enemies, so that object is destroyed when it hits the enemy (and preferably destroys the enemy as well).

Comment
Add comment · Show 2 · 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 Agent_Cooper · May 13, 2015 at 01:47 PM 0
Share

thanks your code worked, but now i have a problem i can't fix: the fireball doesn't move when instantiated

avatar image Eudaimonium · May 13, 2015 at 03:30 PM 0
Share

If your code for instantiating the fireball hasn't changed, you probably forgot to drag the new prefab (fireball object with new script!) into the fireBall GameObject field of your Enemy (SkullTower) script.

Your new fireball prefab also needs to have a RigidBody attached, as well as a collider box, and don't forget to set correct tags/layers if you use any!

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Destroy bullet on random collision 2 Answers

Have bullets be destroyed when they collide with ANY collider 0 Answers

How do you make a bullet disappear upon contact with another object 2 Answers

Destroy Turret with machine Gun 0 Answers

Destroy Bullet On 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