Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 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
1
Question by luniac · Aug 20, 2012 at 02:34 AM · addforceontriggerenterfixedupdate

OnTriggerEnter AddForce OK???

Is it okay to use the AddForce function in the OnTriggerEnter function or is it better to use a flag instead, that will activate AddForce in the FixedUpdate() function??

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
2

Answer by Bunny83 · Aug 20, 2012 at 02:49 AM

It's absolutely fine to use it in OnTriggerEnter since it's a one-time-event. Keep in mind that you might want to use a different ForceMode, usually Impulse or VelocityChange.

If you want to apply a force over time, it should be done in FixedUpdate.

edit

An very easy solution is to use a booster script which you attach to your object when it should be boosted. This component would remove itself after the boost is over. It's also possible to use a prefab which contains a boost script that you would attach as child to the boosted object. This as the additional advantage that you can add particle / sound effects to the prefab and you get the effects for free.

Here's the component solution

I guess you use UnityScript?

 // Booster.js
 var boost : Vector3;
 var boostTime : float;
 
 function Start()
 {
     Destroy(this, boostTime);
 }
 
 function FixedUpdate()
 {
     rigidbody.AddForce(boost * Time.deltaTime);
 }

On your "boosting device" script

 var force : float = 10.0f;
 var boostTime : float = 1.3f;
 function OnTriggerEnter(other : Collider)
 {
     if(other.rigidbody == null)
         return;
     var boost = other.AddComponent(Booster);
     boost.boost = transform.forward * force;
     boost.boostTime = boostTime;
 }

This will add a "booster" component to every rigidbody that enters the trigger. It will set the boost direction / force and how long it should be boosted.

The component will destroy itself after the boosting time. As long as it's attached it will apply a constant force to the RB which will accelerate it continuously. Keep in mind that you have to configure the boosting time and the force in the inspector to match your desired acceleration.

Comment
Add comment · Show 8 · 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 luniac · Aug 20, 2012 at 01:01 PM 0
Share

So if i have something like this i should put it in fixedupdate?:

collision.gameObject.rigidbody.AddForce(transform.forward -force.1); Debug.Log("1"); yield WaitForSeconds(.1); collision.gameObject.rigidbody.AddForce(transform.forward -force.2); Debug.Log("2"); yield WaitForSeconds(.1); collision.gameObject.rigidbody.AddForce(transform.forward -force.3); Debug.Log("3"); yield WaitForSeconds(.1); collision.gameObject.rigidbody.AddForce(transform.forward -force.4); Debug.Log("4"); yield WaitForSeconds(.1); collision.gameObject.rigidbody.AddForce(transform.forward -force.5); Debug.Log("5"); yield WaitForSeconds(.1); collision.gameObject.rigidbody.AddForce(transform.forward -force.6); Debug.Log("6"); yield WaitForSeconds(.1); collision.gameObject.rigidbody.AddForce(transform.forward -force.7); Debug.Log("7"); yield WaitForSeconds(.1); collision.gameObject.rigidbody.AddForce(transform.forward -force.8); Debug.Log("8"); yield WaitForSeconds(.1); collision.gameObject.rigidbody.AddForce(transform.forward -force.9); Debug.Log("9");Debug.Log(collision.gameObject.rigidbody.velocity); yield WaitForSeconds(.1); collision.gameObject.rigidbody.AddForce(transform.forward -force*1); Debug.Log("10");Debug.Log(collision.gameObject.rigidbody.velocity); yield WaitForSeconds(.1); collision.gameObject.rigidbody.AddForce(transform.forward -force*1); Debug.Log("11");Debug.Log(collision.gameObject.rigidbody.velocity); yield WaitForSeconds(.1); collision.gameObject.rigidbody.AddForce(transform.forward -force*1); Debug.Log("12");Debug.Log(collision.gameObject.rigidbody.velocity); yield WaitForSeconds(.1); collision.gameObject.rigidbody.AddForce(transform.forward -force*1); Debug.Log("13");Debug.Log(collision.gameObject.rigidbody.velocity);

avatar image luniac · Aug 20, 2012 at 01:02 PM 0
Share

thats how i made a boost platform. I tried other forcemodes but the original worked best.

avatar image luniac · Aug 20, 2012 at 01:03 PM 0
Share

the impulse and velocity change forcemodes were way to powerful, the object moved way too fast.

avatar image Bunny83 · Aug 20, 2012 at 07:29 PM 0
Share

That's simply because your force is probably way to high ;) The normal forcemode is Acceleration, which is ment to be executed once per physics frame (aka FixedUpdate). All forcemodes do exactly the same, they result in a velocity change. However Impulse is taking the RB mass into account where Velocity change just directly change the velocity.

Force and acceleration are ment to be applied every frame to simulate a constant force which results in a constant acceleration. Here it's the same, Force is using thee RBs mass where acceleration just directly applies the acceleration.

The difference between acceleration and velocity change is the unit of the value. The velocity change is in meter / sec. so a value that is directly added to the velocity. If you pass the value 5, the velocity will be increased by 5.

The unit of acceleration is "meter / sec. squared" So this is basically the velocity change which would happen when you apply this force for 1 sec. It specified how much m / s are added per sec. This value is only correct when used in FixedUpdate which ensures the sum of each call matches the desired value per sec.

You probably use an incredible high acceleration / force, but you apply this force only for a fraction of a sec.

If you really want this stepped increase you should keep it in OnTriggerEnter. FixedUpdate can't even be a coroutine so you can't use yield in there ;)

ps: Don't forget to remove the Debog.Logs when you don't need them anymore since they are really slow when testing in the editor. Even in a build it will cause your output_log to grow.

avatar image luniac · Aug 20, 2012 at 09:04 PM 0
Share

Thank you for the well written comment. I do want the force to take into account the objects' mass, which the original force does do.

Also it didn't occur to me that fixedupdate does not do coroutines which would have answered my original question...

Is there perhaps a better way to create a boost platform using some different forcemode and no yield?

It seems impulse wont work because it would result in an erratic application of force since its instantaneous, and velocity change doesnt take mass into account.

The solution i have is the only one i could really think of at the time. I thought i was quite clever actually :) By making the force slowly increase, the motion is smoothed out pretty good.

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

9 People are following this question.

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

Moving an object using rigidbody.AddForce & keyboard input. 1 Answer

Mistake in Function with Collision and AddForce 0 Answers

AddForce in Update 1 Answer

Increase Speed 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