Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 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
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 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
1
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
Add comment · 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 ins$$anonymous$$d 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, Force$$anonymous$$ode.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, Force$$anonymous$$ode.Impulse );

or

 coin.rigidbody.AddRelativeForce( Vector3.up * power, Force$$anonymous$$ode.Impulse );

or

 coin.rigidbody.AddRelativeForce( 0, power, 0, Force$$anonymous$$ode.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
 // Force$$anonymous$$ode.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.

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

Rigidbody.Addforce not working in Unity 5.4.1 3 Answers

Creating a teleportation gun 1 Answer

OnTriggerEnter AddForce 1 Answer

AddForce to an object with static velocity 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