|
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??
(comments are locked)
|
|
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? On your "boosting device" script 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. 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);
Aug 20 '12 at 01:01 PM
luniac
thats how i made a boost platform. I tried other forcemodes but the original worked best.
Aug 20 '12 at 01:02 PM
luniac
the impulse and velocity change forcemodes were way to powerful, the object moved way too fast.
Aug 20 '12 at 01:03 PM
luniac
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.
Aug 20 '12 at 07:29 PM
Bunny83
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.
Aug 20 '12 at 09:04 PM
luniac
(comments are locked)
|
