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 MrHezelbeef · Aug 25, 2013 at 03:30 AM · instantiategravityontriggerenterprojectiletags

My instanced projectiles will not trigger the OnTriggerEnter command on enemies

Hi!

New to this, getting a little frustrated and wondering why it won't work...

Have these projectiles that are being instanced when I click the screen, the code on the game is as follows:

 public Rigidbody banana;
     public float shotForce = 1000f;
     public Transform shotPos;
     
     void Update () {
         
         if(Input.GetMouseButtonDown(0))
         {    
             Rigidbody shot = Instantiate(banana, shotPos.position, shotPos.rotation) as Rigidbody;
             shot.AddForce(shotPos.forward * shotForce);
         }
     
     }
 }

The code that is attached to the "enemies" that should destroy said enemies when the projectiles enter their collider is:

 private GameObject banana;
     
     void Awake ()
     {
        
         banana = GameObject.FindGameObjectWithTag(Tags.banana);
        
     }
     
     
      void OnTriggerEnter(Collider other) {
     
         if(other.gameObject == banana)
         {
         
         //Instantiate (explosion, transform.position, transform.rotation);
         
         Destroy(gameObject);
         }
     }
 }

and all of this is working with a tag manager which is in the scripts folder which is simply :

 public const string banana = "Banana";
 public const string player = "Player";

The projectile prefabs collider has Is Trigger unchecked. The projectile prefabs rigidbody has Use Gravity Checked and Is Kinematic unchecked.

The enemy prefabs collider has Is Trigger checked. The enemy prefabs rigidbody has Use Gravity unhecked and Is Kinematic checked

The strange thing is that when I put a projectile prefab in the scene and let it fall onto the enemy it works, but when I am shooting the instanced ones at them it doesn't.

I then tried putting an empty game object that instanced without having to shoot, it just keeps instancing projectiles with:

 public Rigidbody banana;
 public Transform shotPos;
     
     void Update () {
         
             
             Rigidbody shot = Instantiate(banana, shotPos.position, shotPos.rotation) as Rigidbody;
             
         }
 }

and it instances one on top of the other...but they do not fall with gravity, like the original that I put into the scene by hand, they just float there!

what is it that is wrong? please help!

Comment
Add comment · Show 2
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 MrHezelbeef · Aug 25, 2013 at 11:02 AM 0
Share

gotto bump it....really need urgent help!

avatar image sethi-sahil27 · Apr 09, 2016 at 11:08 PM 0
Share

did you find a solution to this?

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by neurograph · Sep 02, 2013 at 10:49 AM

Is your problem about physics or about triggering? I don't know, but for the target triggering I think you don't need the Awake function in your enemy script... Have you tried simply adding .tag to the other collider in the OnTriggerEnter function??

 void OnTriggerEnter(Collider other) {
  
        if(other.gameObject.tag == "banana")
         {
  
        //Instantiate (explosion, transform.position, transform.rotation);
             
      
        Destroy(gameObject);
        }
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
0

Answer by Eudaimonium · Apr 10, 2016 at 10:44 AM

I do not understand the first part of your code - you are instantiating a Rigidbody? Why? Rigidbody has no colliders. When you drop a prefab into the scene, it's probably a complete GameObject with colliders, rigidbody and script.

Instead of public RigidBody banana;, use:

 public GameObject bananaPrefab;

Assign the prefab via the Inspector.

Then, instantiate the gameobject:

 GameObject shot = Instantiate(bananaPrefab, shotPos.position, shotPos.rotation) as GameObject;
              shot.rigidBody.AddForce(shotPos.forward * shotForce);


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

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

18 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

Related Questions

Make multiple Projectiles move at the same Time... 0 Answers

Fire Projectile based on Model rotation 1 Answer

Velocity and AddForce Problems 0 Answers

Applying force to a rigidbody 2 Answers

Prefab tag bug 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