Playmaker Forum

PlayMaker Help & Tips => PlayMaker Help => Topic started by: polygon on September 11, 2014, 05:15:18 AM

Title: Limit Rigid Body Velocity
Post by: polygon on September 11, 2014, 05:15:18 AM
Hi :)

I need to set a limit for the maximal velocity of a rigid body. The rigid body is a 3d object and can move in all directions (+-x, +-y and +-z). On the unity forums I found a script that exactly does this and I tried to recreate it in playmaker. Here's the script (originally created by user "whydoidoit")

Quote
     public float maxSpeed = 200f;//Replace with your max speed
     
    void FixedUpdate()
    {
    if(rigidbody.velocity.magnitude > maxSpeed)
    {
    rigidbody.velocity = rigidbody.velocity.normalized * maxSpeed;
    }
    }

I am able to create a FSM with a "Get Component" Action that gets the "rigidbody.velocity.magnitude".
This magnitude gets stored in a float variable and I check via float compare if the magnitude has reached my defined max speed. But the tricky part comes now: I can't figure out how to set "rigidbody.velocity.normalized" via "Set Property" action in playmaker. I don't have access to this command because it doesn't show up in the list of available properties :-/

Thanks for your help and best regards,
Daniel
Title: Re: Limit Rigid Body Velocity
Post by: Lane on September 11, 2014, 07:15:35 AM
Give this a try and let me know if it works.
Title: Re: Limit Rigid Body Velocity
Post by: polygon on September 11, 2014, 09:36:59 AM
Thank you very much for your support!

The attached action doesn't seem to have any influence on the object (I added it as the start state in a new fsm on the rigid body object) but the velocity doesn't clamp.

I changed "public override void OnFixedUpdate()" to "public override void OnUpdate()" and this works now. But I'm not sure if this is a good way to get things done because the unity documentation says that FixedUpdate should be used with rigid bodies, as you did it in your script.

I'm a bit confused why it doesn't work with FixedUpdate  :-\

Best regards,
Daniel

By the way: The script I quoted in my initial post works and it's using FixedUpdate too. Quite confusing...
Title: Re: Limit Rigid Body Velocity
Post by: Lane on September 11, 2014, 10:16:14 AM
I'll look into it a little more.

It's alright to use update but the oscillation between fixed update frames and update frames will probably cause some stuttering when it hits the velocity cap.. If it's not notice then it's not a problem.
Title: Re: Limit Rigid Body Velocity
Post by: polygon on September 11, 2014, 10:48:42 AM
Thanks for your help :) I'm looking forward to hear from you :)

Title: Re: Limit Rigid Body Velocity
Post by: Lane on September 17, 2014, 03:30:24 PM
Forgot to add an Awake function for using FixedUpdate.

Should work now.
Title: Re: Limit Rigid Body Velocity
Post by: polygon on September 18, 2014, 04:51:35 PM
Thank you very much! It works fine :)