playMaker

Author Topic: Framerate Independent Projectile Movement? [SOLVED]  (Read 696 times)

LordHorusNL

• Beta Group
• Full Member
• Posts: 123
Framerate Independent Projectile Movement? [SOLVED]
« on: September 16, 2017, 04:55:34 PM »
Hey guys i've got a projectile with a rigidbody that i'm propelling with AddForce and i need the movement to be independent of the framerate.

I know AddForce runs in FixedUpdate, however i'm not setting the force every frame because that seems to cause acceleration and i need my projectile to fly at a constant speed (80 meters per second)

How can i achieve this?

« Last Edit: September 17, 2017, 09:03:26 AM by LordHorusNL »

Deek

• Full Member
• Posts: 114
Re: Framerate Independent Projectile Movement?
« Reply #1 on: September 17, 2017, 05:31:31 AM »
If you really want to have a constant speed you need to set the 'Force Mode' in Add Force to "Acceleration" and run it every frame. This doesn't add up the speed like "Force" or "Impulse" would, but rather keep the specified speed at a constant level.

Velocity is like a one-time push in the chosen direction while acceleration is like the speed of a car, constant and controllable.

LordHorusNL

• Beta Group
• Full Member
• Posts: 123
Re: Framerate Independent Projectile Movement?
« Reply #2 on: September 17, 2017, 06:05:00 AM »
I tried that before, however when using acceleration the projectile no longer flies in a straight line but rather bends downwards the moment it leaves the barrel.
Like it's somehow ignoring the input form TransformDirection.

I always found using AddForce to be a hassle, it never seems to do what you expect.

Deek

• Full Member
• Posts: 114
Re: Framerate Independent Projectile Movement?
« Reply #3 on: September 17, 2017, 08:18:40 AM »
That's because you use gravity on that RigidBody.
Gravity (by default) uses the same process of utilizing AddForce (you can simulate the Gravity in unity by having a script with the line
Code: [Select]
`this.gameObject.GetComponent<RigidBody>().AddForce(Vector3(0, -9.81, 0), ForceMode.Acceleration);` in FixedUpdate, so you see that Gravity is also a constant force downwards (Y Position), while you add another constant force in the Z Position.
They don't negate each other but add up, which means that you combine both forces, resulting in a forward+downward movement.

You either need to disable Gravity on that RigidBody, set it to be Kinematic after adding force to it once (but then it also ignores collision) or use the action "Set Velocity" (should be equal to AddForce's "Velocity Change") and run that in world space every frame.

LordHorusNL

• Beta Group
• Full Member
• Posts: 123
Re: Framerate Independent Projectile Movement?
« Reply #4 on: September 17, 2017, 08:40:28 AM »
DOhhhhh! i was already running with gravity off on the rigidbody, but forgot to disable the SetGravity action in my FSM

SetVelocity already runs in FixedUpdate so that did the trick!

Thanks
« Last Edit: September 17, 2017, 09:02:58 AM by LordHorusNL »