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 XxRQxX · Apr 11, 2015 at 09:58 AM · movementshootingscrpting

Shoot script not working anymore....

So I messed with my Bullet script so that it would instantiate an explosion prefab when it collided with something/ after 5 seconds if it didn't... and somehow now ONLY the explosion instantiates and not the bullet itself, when I fire the bullet doesn't move anymore and so just explodes on itself. If anyone could tell me what I did wrong that would be really appreciated.

Here is the Shooting script sent on an empty character where the bullet should spawn:

 public class Shoot : MonoBehaviour
 {
     public Rigidbody2D Bullet;              
     public float speed = 20f;               
 
     
     
     private PlayerControl playerCtrl;       
     private Animator anim;                  
     
     void Awake()
     {
         anim = transform.root.gameObject.GetComponent<Animator>();
         playerCtrl = transform.root.GetComponent<PlayerControl>();
     }
     
     
     void Update ()
     {
         // If the fire button is pressed...
         if(Input.GetButtonDown("Fire1"))
         {
             
             anim.SetTrigger("Shoot");
 
             
             // If the player is facing right...
             if(playerCtrl.facingRight)
             {
                 // ... instantiate the bullet facing right and set it's velocity to the right. 
                 Rigidbody2D bulletInstance = Instantiate(Bullet, transform.position, Quaternion.Euler(new Vector3(0,0,0))) as Rigidbody2D;
                 bulletInstance.velocity = new Vector2(speed, 0);
             }
             else
             {
                 // Otherwise instantiate the bullet facing left and set it's velocity to the left.
                 Rigidbody2D bulletInstance = Instantiate(Bullet, transform.position, Quaternion.Euler(new Vector3(0,0,180f))) as Rigidbody2D;
                 bulletInstance.velocity = new Vector2(-speed, 0);
             }
         }
     }
 }

And here is the Bullet script itself with the Explosion prefab

 public class Bullet : MonoBehaviour 
 {
     public GameObject Explosion;        // Prefab of explosion effect.
     
     
     void Start () 
     {
         
         Destroy(gameObject, 5);
     }
     
     
     void OnExplode()
     {
         // Create a quaternion with a random rotation in the z-axis.
         Quaternion randomRotation = Quaternion.Euler(0f, 0f, Random.Range(0f, 360f));
         
         .
         Instantiate(Explosion, transform.position, randomRotation);
     }
     
     void OnTriggerEnter2D (Collider2D col) 
     {
         // If it hits an enemy...
         if(col.tag == "Enemy")
         {
             //  Enemy script and call the Hurt function.
             col.gameObject.GetComponent<Enemy>().Hurt();
             
             
             OnExplode();
             
             
             Destroy (gameObject);
         }
 
         {
             
             OnExplode();
             Destroy (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

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by KalleH · Apr 11, 2015 at 03:03 PM

Probably your bullet hits some collider like the player itself while instantiated which causes the explosion to be instantiated but at the same time the bullet itself is destroyed (lines 37-41). To fix this you could for example create a layer for objects that you want the bullet to collide with and then in OnTriggerEnter2D check if the other collider has this layer.

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 DoTA_KAMIKADzE · Apr 11, 2015 at 12:00 PM

I guess your second script lines #37-41 get called on spawn, because probably your "transform.position" in your first script is a player position I guess and your bullet instantly hits your player on spawn. To verify my guess add this Debug line:

  {
      Debug.Log(col.name);//this should tell you what is being hit, most likely player
      OnExplode();
      Destroy (gameObject);
  }

In order to overcome this, you have a lot of options, I'll just write here few easiest ones over here:

1)The simplest one:

  if(col.tag != "Player")//or check by player name like I did in Debug or whatever else specific to player
  {
       OnExplode();
       Destroy (gameObject);
  }

2.1)The way I DO NOT recommend - add a "spawn timer" that will count few seconds before it gets out of player and only then be able to hit something.

2.2.)Second way that is more or less OK - same "spawn timer" or TriggerExit that will be affected only by player, if anything else is hit meanwhile then hit it.

3)Spawn outside of the player, add an empty object as a child to player and call it spawn position and then just instantiate bullet transform from its transform.

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 XxRQxX · Apr 13, 2015 at 09:29 PM 0
Share

I went with number 3- works great thank you =]

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

Bullet stops when new bullet instantiates pls help 3 Answers

Character has free movement when 'shooting' 0 Answers

How to to not move when shooting? 0 Answers

Problem with shooting an object in 2D 1 Answer

why wont my fire point flip? 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