playMaker

Author Topic: RigidBody set Constraints  (Read 10594 times)

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
RigidBody set Constraints
« on: July 05, 2012, 03:11:00 AM »
Hi,

 Following a requests: http://hutonggames.com/playmakerforum/index.php?topic=1845.msg8130#msg8130

 Please find an action that will allow you to constraint a rigidbody movement in position or rotation for each axis.

 I haven't made this action as something you can use every frame, I think it should be use once to set it up, if you however feel the need to use it every frame, let me know, and I'll update the action.

Code: [Select]
// (c) Copyright HutongGames, LLC 2010-2012. All rights reserved.

using UnityEngine;

namespace HutongGames.PlayMaker.Actions
{
[ActionCategory(ActionCategory.Physics)]
[Tooltip("Sets the constraints of a rigidBody")]
public class SetRigidBodyConstraints : FsmStateAction
{
[RequiredField]
[CheckForComponent(typeof(Rigidbody))]
public FsmOwnerDefault gameObject;

public FsmBool freezePositionX;

public FsmBool freezePositionY;

public FsmBool freezePositionZ;

public FsmBool freezeRotationX;

public FsmBool freezeRotationY;

public FsmBool freezeRotationZ;

public override void Reset()
{
gameObject = null;
freezePositionX = false;
freezePositionY = false;
freezePositionZ = false;

freezeRotationX = false;
freezeRotationY = false;
freezeRotationZ = false;

}

public override void OnEnter()
{
DoSetConstraints();


Finish();
}

void DoSetConstraints()
{
GameObject go = Fsm.GetOwnerDefaultTarget(gameObject);
if (go == null)
{
LogError("gameObject is null");
return;
}


if (go.rigidbody == null)
{
LogError("RigidBody Component required");
return;
}

go.rigidbody.constraints = RigidbodyConstraints.None;
if (freezePositionX.Value)
{
go.rigidbody.constraints = go.rigidbody.constraints | RigidbodyConstraints.FreezePositionX ;
}
if (freezePositionY.Value)
{
go.rigidbody.constraints = go.rigidbody.constraints | RigidbodyConstraints.FreezePositionY ;
}
if (freezePositionZ.Value)
{
go.rigidbody.constraints = go.rigidbody.constraints | RigidbodyConstraints.FreezePositionZ ;
}

if (freezeRotationX.Value)
{
go.rigidbody.constraints = go.rigidbody.constraints | RigidbodyConstraints.FreezeRotationX ;
}
if (freezeRotationY.Value)
{
go.rigidbody.constraints = go.rigidbody.constraints | RigidbodyConstraints.FreezeRotationY ;
}
if (freezeRotationZ.Value)
{
go.rigidbody.constraints = go.rigidbody.constraints | RigidbodyConstraints.FreezeRotationZ ;
}
}
}
}

Bye,

 Jean
 

Horror

  • Junior Playmaker
  • **
  • Posts: 79
Re: RigidBody set Constraints
« Reply #1 on: July 05, 2012, 06:07:28 AM »
Awesome! Thanks Jean  ;D

Alatriste

  • Full Member
  • ***
  • Posts: 195
Re: RigidBody set Constraints
« Reply #2 on: January 29, 2014, 10:03:22 AM »
Thanks for this, Jean!

Can you tell me how to do it to perform the action every frame? This is particularly useful when you have your character colliding with something in one of the axis, so you want to constraint the movement in that particular axis, but still allow movement in the other ones.

Cheers!

Dev_Sebas

  • 1.2 Beta
  • Sr. Member
  • *
  • Posts: 398
    • Blog
Re: RigidBody set Constraints
« Reply #3 on: January 29, 2014, 11:42:37 AM »
Thx!

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: RigidBody set Constraints
« Reply #4 on: January 30, 2014, 04:33:38 AM »
Hi,

 I would simply create an event base switch system as opposed to an everyframe option, which I think would affect performances.

 Bye,

 Jean

Alatriste

  • Full Member
  • ***
  • Posts: 195
Re: RigidBody set Constraints
« Reply #5 on: January 31, 2014, 03:31:51 AM »
Noted. I´ll give it a shot!

Thanks!

Twood

  • Junior Playmaker
  • **
  • Posts: 76
Re: RigidBody set Constraints
« Reply #6 on: September 17, 2016, 08:17:18 PM »
This action doesn't seem to load without errors in newer versions of unity... (unless something else is weird but all I did was call it SetRigidBodyConstraints.cs and put in the custom actions/physics folder) any chance of an update?

edit: actually I don't need it because my problem was I couldn't figure out how to completely wipe constraints, but I figured out you have to set at least one rotation constraint (that will wipe out any position constraints) and THEN you can use the uncheck on the freeze rotation and everything will be clear.
« Last Edit: September 17, 2016, 10:36:05 PM by Twood »

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: RigidBody set Constraints
« Reply #7 on: October 12, 2016, 05:28:21 AM »
Hi,

The action works, but I think you simply did not let unity update the code base, usually it warns you about it.

Anyway, I corrected it so that Unity doesn't need to do anything, and it's now on the Ecosystem

Bye,

 Jean