• 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 RGI · Mar 22, 2013 at 02:21 PM · javascriptgameobjectrigidbodyaddforceontriggerenter

How do I add force in the on trigger

Hi, I have this simple problem, I have an object and a group of objects (prefab) named coin. All I have to do is addforce to the coins when they enter the main object. The collision/trigger thingy works, when I change the "coin.rigidbody.AddRelativeForce (0, power, 0);" line, it works so the problem is with that line.

 var coin : GameObject;
 static var speed : int;
 var power;
 
 
 
 function Start () {
 InvokeRepeating("checking", 0, 0.3);
 }
 
 function OnTriggerEnter ( collision : Collider){
 
  
  coin.rigidbody.AddRelativeForce (0, power, 0);
 
 }
 
 function checking (){
 power = speed/3;
 
 
 }

(the var power is set in other script

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 moghes · Mar 22, 2013 at 02:27 PM 0
Share

are the coins who enter the trigger? then you could use

function OnTriggerEnter ( collision : Collider){

collision .rigidbody.AddRelativeForce (0, power, 0);

}

And are you sure you are giving a value to power before you use it? Do you see errors in the console?

avatar image Chris12345 · Mar 22, 2013 at 02:28 PM 0
Share

are you instantiating objects, because if you are it will name it Coin(clone) and not coin so you will have to make a script to rename it on start.

1 Reply

· Add your reply
  • Sort: 
avatar image
Best Answer

Answer by AlucardJay · Mar 22, 2013 at 02:29 PM

The problem is you want to add an instantaneous force, where AddForce is applied over time in physics. For an instantaneous force in your case of entering a trigger volume, you need to use ForceMode.Impulse :

 rigidbody.AddForce( transform.up * power, ForceMode.Impulse );

Links :

http://docs.unity3d.com/Documentation/ScriptReference/Rigidbody.AddForce.html

http://docs.unity3d.com/Documentation/ScriptReference/ForceMode.Impulse.html

Comment
clunk47
azicii

People who like this

2 Show 6 · 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 RGI · Mar 22, 2013 at 02:40 PM 0
Share

there is just one slight error, it moves the main object instead of the coins :D. How do I swap them?

avatar image Polinator · Mar 22, 2013 at 02:44 PM 0
Share

use coin.rigidbody.AddForce( transform.up * power, ForceMode.Impulse );

avatar image AlucardJay · Mar 22, 2013 at 02:46 PM 1
Share

well that was just an example. I see you want to affect the coin? :

 coin.rigidbody.AddForce( transform.up * power, ForceMode.Impulse );

or

 coin.rigidbody.AddRelativeForce( Vector3.up * power, ForceMode.Impulse );

or

 coin.rigidbody.AddRelativeForce( 0, power, 0, ForceMode.Impulse );

Please read the code and see what it is doing.

 // coin : the cached gameObject
 // rigidbody : the component on that gameObject
 // AddForce and AddRelativeForce : check the Unity Scripting Reference
 // transform.up : the up vector in relation to the transform
 // * power : the modifier of the vector
 // ForceMode.Impulse : This Is The Important Part, as detailed in my answer
avatar image RGI · Mar 22, 2013 at 03:07 PM 0
Share

okay, it is working when I set the coin var to a certain object already in game. Problem is, The coins are spawed randomly with a script, so when they are spawned, they dont react with the main object. I forgot to mention that, sry. And another thing is that Iam planning on adding more stuff doing pretty much the same as coins so, I would need the script to activate on trigger with ANY object. Not just coins. Thank you, btw.

avatar image Polinator · Mar 22, 2013 at 09:08 PM 0
Share

Read the documentation on gameObject.tags. All of your issues you have described above will be solved when you learn that.

Show more comments

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.

Update about the future of Unity Answers

Unity Answers content will be migrated to a new Community platform and we are aiming to launch a public beta later in June. Please note, we are aiming to set Unity Answers to read-only mode on the 31st of May in order to prepare for the final data migration.

For more information, please read our full announcement.

Follow this Question

Answers Answers and Comments

14 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

Related Questions

Adding Force to a Rigidbody through OnTriggerEnter 1 Answer

Show message when gameobject hits other gameobject.. 1 Answer

How to use OnTriggerEnter? (JS) 1 Answer

How to disable constant force over time? Rocket game 2 Answers

Dragging object out of position 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