• 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 post has been wikified, any user with enough reputation can edit it.
avatar image
Question by Aggrojag · Apr 12, 2014 at 08:47 AM · gameobjectrigidbodycollideraddforceontriggerenter

Adding Force to a Rigidbody through OnTriggerEnter

This is compliling correctly. The object this is attached to (a boomerang) will go through the object with this script. The object it is hitting is not set to a trigger, and has a rigidbody. I am getting no console errors either. I've also tried this with enemy.gameobject.rigidbody.AddForce and so on... No luck. Thanks in advance!

 using UnityEngine;
 using System.Collections;
 
 public class Enemy : MonoBehaviour {
 
 
     void OnTriggerEnter (Collider enemy){
         if (enemy.tag == "enemy") {
             enemy.rigidbody.AddForce (8000, 2250, 0);
             Destroy (enemy.gameObject, 2f);
 }    
 }
 }
Comment

People who like this

0 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 RyanPaterson · Apr 12, 2014 at 09:35 AM 0
Share

Does your enemy have 'set to trigger' enabled?

avatar image AlucardJay · Apr 12, 2014 at 10:02 AM 0
Share

First thing always with collisions and triggers is to Debug to see what is happening. Add this before your if conditional :

 Debug.Log( gameObject.name + "'s trigger was entered by " + enemy.gameObject.name + " : tag = " + enemy.gameObject.tag );

1 Reply

· Add your reply
  • Sort: 
avatar image
Best Answer

Answer by RyanPaterson · Apr 12, 2014 at 10:01 AM

I tried it myself and got some results. Check your tag is set to 'enemy' instead of 'Enemy' incase there was a caps.

EDIT:Read the comment below about rigid bodies

Comment

People who like this

0 Show 3 · 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 AlucardJay · Apr 12, 2014 at 10:07 AM 0
Share
  • http://docs.unity3d.com/Documentation/ScriptReference/Collider.OnTriggerEnter.html

Note that trigger events are only sent if one of the colliders also has a rigidbody attached. if one, so you only need a trigger one one object, not necessarily the object with the OnTriggerEnter script. Basic rule is 'if it moves, it needs a rigidbody'.

avatar image RyanPaterson · Apr 12, 2014 at 10:11 AM 0
Share

Thank you for the correction, I edited my post.

avatar image Aggrojag · Apr 12, 2014 at 08:38 PM 0
Share

Thank you so much you two! I can't believe it was a due to case sensitivity >_<. I was positive it was the way I wrote the script and never even considered checking the case of 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

23 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

Related Questions

How do I add force in the on trigger 1 Answer

100 OnTriggerEnter Or Tags each player too much? 3 Answers

Audio.PlayOneSHot won't work? 0 Answers

Box collider doesn't work when same size as mesh. 1 Answer

throw object at specific direction or angle 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