playMaker

Author Topic: Limit Rigid Body Velocity  (Read 6184 times)

polygon

  • Playmaker Newbie
  • *
  • Posts: 18
Limit Rigid Body Velocity
« 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

Lane

  • Administrator
  • Hero Member
  • *****
  • Posts: 2511
  • Mender of the past
    • Cleverous
Re: Limit Rigid Body Velocity
« Reply #1 on: September 11, 2014, 07:15:35 AM »
Give this a try and let me know if it works.
« Last Edit: September 17, 2014, 03:30:00 PM by Lane »
Products by Cleverous
|| Vault Core : Database
|| Vault Inventory : Multiplayer Inventory
|| Vault Attributes : Character Stats
|| That Hurt! : Dmg Floaties
|| Quinn : 3D

polygon

  • Playmaker Newbie
  • *
  • Posts: 18
Re: Limit Rigid Body Velocity
« Reply #2 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...
« Last Edit: September 11, 2014, 09:44:22 AM by polygon »

Lane

  • Administrator
  • Hero Member
  • *****
  • Posts: 2511
  • Mender of the past
    • Cleverous
Re: Limit Rigid Body Velocity
« Reply #3 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.
Products by Cleverous
|| Vault Core : Database
|| Vault Inventory : Multiplayer Inventory
|| Vault Attributes : Character Stats
|| That Hurt! : Dmg Floaties
|| Quinn : 3D

polygon

  • Playmaker Newbie
  • *
  • Posts: 18
Re: Limit Rigid Body Velocity
« Reply #4 on: September 11, 2014, 10:48:42 AM »
Thanks for your help :) I'm looking forward to hear from you :)


Lane

  • Administrator
  • Hero Member
  • *****
  • Posts: 2511
  • Mender of the past
    • Cleverous
Re: Limit Rigid Body Velocity
« Reply #5 on: September 17, 2014, 03:30:24 PM »
Forgot to add an Awake function for using FixedUpdate.

Should work now.
Products by Cleverous
|| Vault Core : Database
|| Vault Inventory : Multiplayer Inventory
|| Vault Attributes : Character Stats
|| That Hurt! : Dmg Floaties
|| Quinn : 3D

polygon

  • Playmaker Newbie
  • *
  • Posts: 18
Re: Limit Rigid Body Velocity
« Reply #6 on: September 18, 2014, 04:51:35 PM »
Thank you very much! It works fine :)