• 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
Question by j_basson · May 12, 2013 at 07:52 PM · collisionrigidbodyontriggerenteroncollisionenter

Transform collider not detecting collision on rigidbody collider

I have 2 game objects, an enemy and an axe. My main character throw the axe(which is a rigitbody with a boxcollider) towards an enemy(which has a box collider as well). I'm adding the colliders manually via c# script for the enemy(Transform) and the axe (rigidbody). However the axe just goes straight through the enemy instead of triggering the colliders(I have isTrigger = true). Here is my script.

Here I spawn the enemy and add a box collider(EnemyController.cs) void SpawnTargets() {

         float x = Random.Range(-3.0f, 3.0f);
         Vector3 pos = new Vector3(x, 1f, Random.Range(15.0f,23.0f));
         
         Transform badGuy = (Transform)Instantiate(enemyPrefab, pos, Quaternion.identity);
         //create a box collider and add it to the enemy
         BoxCollider bc = badGuy.gameObject.AddComponent<BoxCollider>();
         bc.center = new Vector3(0,1.2f,0);
         bc.size = new Vector3(1.2f,3,1);
         
         //Physics.IgnoreCollision(badGuy.collider, playerTrans.collider);
         
         enemies.Add(badGuy);
     }

Here is my Shooting.cs scripts(throws the axe)

 void Shoot ()
 {
     if(Input.GetKeyDown(KeyCode.Space))
     {
         //rotate axe so that it's facing forward
         var rotation = Quaternion.Euler(new Vector3(0,90,90));
         //create weapon and fire it
         Rigidbody bulletInstance = Instantiate(bulletPrefab, firePosition.position, rotation) as Rigidbody;
         bulletInstance.AddForce((firePosition.forward) * bulletSpeed);
             
             //add collider
             BoxCollider bc = bulletInstance.gameObject.AddComponent<BoxCollider>();
             bc.center = new Vector3(0.4391576f,0.2372868f,-1.625949e-07f);
             bc.size = new Vector3(1.890458f,0.6338217f,0.159247f);
             bc.isTrigger = true;
             //rotate axe
             bulletInstance.AddTorque(180,0,0);
             
             //destroy ball after 7 seconds
             Destroy(bulletInstance.gameObject,7);
         }
     }
Comment

People who like this

0 Show 0
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
Best Answer

Answer by j_basson · May 17, 2013 at 06:07 PM

Thanks Mexallon , however the reason why the right trigger wasn't being his was because I didn't have a script attached to the gameobject that was dynamically being generated(had to dynamically add the script to the gameboject).

Comment

People who like this

0 Show 0 · 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

Answer by Mexallon · May 12, 2013 at 07:59 PM

Hej there. If you turn "isTrigger" on true on any collider it is not physically effected by other colliders but going through the enemy. Also it does not call the events "OnCollisionEnter" though "OnTriggerEnter" - I don't know which method youre using.

Comment

People who like this

0 Show 5 · 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 j_basson · May 12, 2013 at 08:10 PM 0
Share

Primarily I want to use OnTriggerEnter but it doesn't seem to fire it. I want the axe to trigger the event when it hits the enemy , then I can handle what happens from there on. Right now the only time OnTriggerEnter is firing is when my character gets hit by the enemy. Instead of the axe hitting the enemy as well.

avatar image Mexallon · May 12, 2013 at 08:16 PM 0
Share

I see. You're repositioning the collider of the axe. Did you checked for the colliders to be present and in place in-game?

Edit P.S: its probably easier to use and less faulty to add the collider already to the prefab

avatar image j_basson · May 12, 2013 at 08:19 PM 0
Share

there is a collider on the enemy and on the axe during runtime, I've checked. I turned the isTriggered off and the axe seems to hit the enemy and bounce off, but Instead of bouncing off, I want it to fire the OnTriggerEnter but it won't when I turn the trigger on for the rigidbody collider. The only OnTriggerEntere that gets fired is when my character gets hit by the enemy

avatar image Mexallon · May 13, 2013 at 11:41 AM 0
Share

hm strange. Any Update on that case? I wonder what's the root of the problem

avatar image j_basson · May 13, 2013 at 02:12 PM 0
Share

not really. I'm wondering whether it might not be the hierarchy setup I have that is messing with it. I have the shooting script attached to my main camera which is a child of the main character. When none of the colliders have the triggers enabled then the axe collides with the enemy but when I activate the trigger it still won't register it unless my main character gets hit by the enemy.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

15 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

Related Questions

simple onCollision and onTrigger problem 1 Answer

On Trigger Enter, Collide with object, specific collision 1 Answer

Collision Only being detected on one of the objects involved in the collision - C# 1 Answer

Colliding two GameObjects 1 Answer

what object hit me? easy rigidbody question 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