playMaker

Author Topic: Get/Set Angular Velocity  (Read 10681 times)

jimbobulus

  • Playmaker Newbie
  • *
  • Posts: 2
    • View Profile
Get/Set Angular Velocity
« on: October 26, 2012, 01:25:29 PM »
Hi, I noticed there was no get/set for a rigidbody when I needed it, so I copied and pasted the get/set velocity code to get the angular velocity instead.

So barely any work by me at all! But I hope you guys find them as useful as I did!  :)

GetAngularVelocity

Code: [Select]
using UnityEngine;

namespace HutongGames.PlayMaker.Actions
{
[ActionCategory(ActionCategory.Physics)]
[Tooltip("Gets the Angular Velocity of a Game Object and stores it in a Vector3 Variable or each Axis in a Float Variable. NOTE: The Game Object must have a Rigid Body.")]
public class GetAngularVelocity : FsmStateAction {

[RequiredField]
[CheckForComponent(typeof(Rigidbody))]
public FsmOwnerDefault gameObject;
[UIHint(UIHint.Variable)]
public FsmVector3 vector;
[UIHint(UIHint.Variable)]
public FsmFloat x;
[UIHint(UIHint.Variable)]
public FsmFloat y;
[UIHint(UIHint.Variable)]
public FsmFloat z;
public Space space;
public bool everyFrame;

public override void Reset()
{
gameObject = null;
vector = null;
x = null;
y = null;
z = null;
space = Space.World;
everyFrame = false;
}

public override void OnEnter()
{
DoGetAngularVelocity();

if (!everyFrame)
Finish();
}

public override void OnUpdate()
{
DoGetAngularVelocity();
}

void DoGetAngularVelocity()
{
GameObject go = Fsm.GetOwnerDefaultTarget(gameObject);
if (go == null) return;
if (go.rigidbody == null) return;

Vector3 angularVelocity = go.rigidbody.angularVelocity;

if (space == Space.Self)
angularVelocity = go.transform.InverseTransformDirection(angularVelocity);

vector.Value = angularVelocity;
x.Value = angularVelocity.x;
y.Value = angularVelocity.y;
z.Value = angularVelocity.z;
}
}
}


SetAngularVelocity

Code: [Select]
using UnityEngine;

namespace HutongGames.PlayMaker.Actions
{
[ActionCategory(ActionCategory.Physics)]
[Tooltip("Sets the Angular Velocity of a Game Object. To leave any axis unchanged, set variable to 'None'. NOTE: Game object must have a rigidbody.")]
public class SetAngularVelocity : FsmStateAction
{
[RequiredField]
[CheckForComponent(typeof(Rigidbody))]
public FsmOwnerDefault gameObject;

[UIHint(UIHint.Variable)]
public FsmVector3 vector;

public FsmFloat x;
public FsmFloat y;
public FsmFloat z;

public Space space;

public bool everyFrame;

public override void Reset()
{
gameObject = null;
vector = null;
// default axis to variable dropdown with None selected.
x = new FsmFloat { UseVariable = true };
y = new FsmFloat { UseVariable = true };
z = new FsmFloat { UseVariable = true };
space = Space.Self;
everyFrame = false;
}

public override void OnEnter()
{
DoSetAngularVelocity();

if (!everyFrame)
{
Finish();
}
}

public override void OnFixedUpdate()
{
DoSetAngularVelocity();

if (!everyFrame)
Finish();
}

void DoSetAngularVelocity()
{
var go = Fsm.GetOwnerDefaultTarget(gameObject);
if (go == null || go.rigidbody == null)
{
return;
}

// init position

Vector3 angularVelocity;

if (vector.IsNone)
{
angularVelocity = space == Space.World ?
go.rigidbody.angularVelocity :
go.transform.InverseTransformDirection(go.rigidbody.angularVelocity);
}
else
{
angularVelocity = vector.Value;
}

// override any axis

if (!x.IsNone) angularVelocity.x = x.Value;
if (!y.IsNone) angularVelocity.y = y.Value;
if (!z.IsNone) angularVelocity.z = z.Value;

// apply

go.rigidbody.angularVelocity = space == Space.World ? angularVelocity : go.transform.TransformDirection(angularVelocity);
}

}
}

jerware

  • Playmaker Newbie
  • *
  • Posts: 23
    • View Profile
Re: Get/Set Angular Velocity
« Reply #1 on: May 04, 2013, 07:20:39 PM »
A belated thanks for these! I came here looking for exactly this.

taforyou

  • Playmaker Newbie
  • *
  • Posts: 14
    • View Profile
Re: Get/Set Angular Velocity
« Reply #2 on: October 06, 2013, 12:41:55 AM »
i didn't mean to dig this topic up again. But I just want to say "Thank you" :) i'm looking for this action :)

klvo

  • Playmaker Newbie
  • *
  • Posts: 13
    • View Profile
Re: Get/Set Angular Velocity
« Reply #3 on: January 29, 2014, 12:44:41 AM »
Hi, thanks for these adaptations.

These actions don't seem to work with the new Rigidbody 2d.
I add torque2d to an object with a rigidbody2d, but when I use GetAngularVelocity I get 0 on all three axis, both on world and local space.

Is it possible to adapt the actions to the new 2d system?

SynthMan

  • Playmaker Newbie
  • *
  • Posts: 4
    • View Profile
Re: Get/Set Angular Velocity
« Reply #4 on: March 10, 2014, 09:53:34 AM »
I'm using the 2D system and want to have an object's Z rotation change from 0 to -90 degrees based on it's downward velocity (example:  nose of plane dips towards ground faster it falls).  I have a working script, but wanted to know how to translate this to Playmaker.  Not sure which Actions / Variables I need in PM.  I see no Euler options, etc.

float maxSpeed = 3f;

velocity = Vector3 (velocity, maxSpeed);

transform.position += velocity * Time.deltaTime;

float angle = 0;
if (velocity.y < 0) {
   angle = Mathf.Lerp(0, -90, -velocity.y);
}

transform.rotation = Quaternion.Euler(0, 0, angle);

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15318
  • Official Playmaker Support
    • View Profile
Re: Get/Set Angular Velocity
« Reply #5 on: March 11, 2014, 03:22:05 AM »
Hi,

 I created a whole set of Custom Actions to work with Quaternions, then you can do that euler to quaternion procedure.

https://hutonggames.fogbugz.com/default.asp?W967

bye,

 Jean

Twood

  • Playmaker Newbie
  • *
  • Posts: 27
    • View Profile
Re: Get/Set Angular Velocity
« Reply #6 on: November 06, 2014, 11:20:17 AM »
Pretty essential if you're moving around/pooling rigid bodies so thanks for this

Mulbin

  • Junior Playmaker
  • **
  • Posts: 84
    • View Profile
Re: Get/Set Angular Velocity
« Reply #7 on: June 05, 2016, 04:52:28 AM »
Sorry to drag up such an old thread, but I really need these scripts... but Unity says they are obsolete.  Any chance of an updated version?

Mulbin

  • Junior Playmaker
  • **
  • Posts: 84
    • View Profile
Re: Get/Set Angular Velocity
« Reply #8 on: June 05, 2016, 06:53:12 AM »
For more detail on the problem, this is what Visual Studio throws up.  Any help would be awesome, really surprised this isn't a standard function of Playmaker.

Code: [Select]
Error CS0619 'GameObject.rigidbody' is obsolete: 'Property rigidbody has been deprecated. Use GetComponent<Rigidbody>() instead. (UnityUpgradable)' Go For Launch Pre Alpha 0.1.CSharp C:\Users\Joe\Documents\Go For Launch Pre Alpha 0.1\Assets\GetAngularVelocity.cs 52 Active
Error CS0619 'GameObject.rigidbody' is obsolete: 'Property rigidbody has been deprecated. Use GetComponent<Rigidbody>() instead. (UnityUpgradable)' Go For Launch Pre Alpha 0.1.CSharp C:\Users\Joe\Documents\Go For Launch Pre Alpha 0.1\Assets\GetAngularVelocity.cs 54 Active
Error CS1061 'Component' does not contain a definition for 'angularVelocity' and no extension method 'angularVelocity' accepting a first argument of type 'Component' could be found (are you missing a using directive or an assembly reference?) Go For Launch Pre Alpha 0.1.CSharp C:\Users\Joe\Documents\Go For Launch Pre Alpha 0.1\Assets\GetAngularVelocity.cs 54 Active
Warning CS0108 'AudioMute.audio' hides inherited member 'Component.audio'. Use the new keyword if hiding was intended. Go For Launch Pre Alpha 0.1.CSharp C:\Users\Joe\Documents\Go For Launch Pre Alpha 0.1\Assets\AudioMute.cs 7 Active
Warning CS0108 'SplashController.animation' hides inherited member 'Component.animation'. Use the new keyword if hiding was intended. Go For Launch Pre Alpha 0.1.CSharp C:\Users\Joe\Documents\Go For Launch Pre Alpha 0.1\Assets\Tales From The Rift\Splash\SplashController.cs 10 Active
Warning CS0618 'Renderer.castShadows' is obsolete: 'Property castShadows has been deprecated. Use shadowCastingMode instead.' Go For Launch Pre Alpha 0.1.CSharp.Plugins C:\Users\Joe\Documents\Go For Launch Pre Alpha 0.1\Assets\Plugins\PuffySmoke\Scripts\Puffy_Renderer.cs 1815 Active
Warning CS0618 'Material.Material(string)' is obsolete: 'Creating materials from shader source string will be removed in the future. Use Shader assets instead.' Go For Launch Pre Alpha 0.1.CSharp C:\Users\Joe\Documents\Go For Launch Pre Alpha 0.1\Assets\CameraPath3\Scripts\Util\CameraDeAlpha.cs 11 Active
Warning CS0618 'Application.LoadLevel(string)' is obsolete: 'Use SceneManager.LoadScene' Go For Launch Pre Alpha 0.1.CSharp C:\Users\Joe\Documents\Go For Launch Pre Alpha 0.1\Assets\CircularGravityForce Package\Scrips\Misc\SceneSettings.cs 112 Active
Warning CS0618 'Application.loadedLevelName' is obsolete: 'Use SceneManager to determine what scenes have been loaded' Go For Launch Pre Alpha 0.1.CSharp C:\Users\Joe\Documents\Go For Launch Pre Alpha 0.1\Assets\CircularGravityForce Package\Scrips\Misc\SceneSettings.cs 112 Active
Warning CS0618 'Application.LoadLevel(string)' is obsolete: 'Use SceneManager.LoadScene' Go For Launch Pre Alpha 0.1.CSharp C:\Users\Joe\Documents\Go For Launch Pre Alpha 0.1\Assets\CircularGravityForce Package\Scrips\Misc\SceneSettings.cs 194 Active
Warning CS0618 'Application.LoadLevel(int)' is obsolete: 'Use SceneManager.LoadScene' Go For Launch Pre Alpha 0.1.CSharp C:\Users\Joe\Documents\Go For Launch Pre Alpha 0.1\Assets\PuffySmoke sample scenes\Scripts\Others\GuiControls.cs 50 Active
Warning CS0618 'Application.loadedLevel' is obsolete: 'Use SceneManager to determine what scenes have been loaded' Go For Launch Pre Alpha 0.1.CSharp C:\Users\Joe\Documents\Go For Launch Pre Alpha 0.1\Assets\PuffySmoke sample scenes\Scripts\Others\GuiControls.cs 50 Active
Warning CS0618 'Application.levelCount' is obsolete: 'Use SceneManager.sceneCountInBuildSettings' Go For Launch Pre Alpha 0.1.CSharp C:\Users\Joe\Documents\Go For Launch Pre Alpha 0.1\Assets\PuffySmoke sample scenes\Scripts\Others\GuiControls.cs 50 Active
Warning CS0618 'Application.LoadLevel(int)' is obsolete: 'Use SceneManager.LoadScene' Go For Launch Pre Alpha 0.1.CSharp C:\Users\Joe\Documents\Go For Launch Pre Alpha 0.1\Assets\Screen Fader Pack\Code\Fader.cs 1096 Active
Warning CS0618 'Application.LoadLevel(string)' is obsolete: 'Use SceneManager.LoadScene' Go For Launch Pre Alpha 0.1.CSharp C:\Users\Joe\Documents\Go For Launch Pre Alpha 0.1\Assets\Screen Fader Pack\Code\Fader.cs 1098 Active
Warning CS0618 'Application.loadedLevel' is obsolete: 'Use SceneManager to determine what scenes have been loaded' Go For Launch Pre Alpha 0.1.CSharp C:\Users\Joe\Documents\Go For Launch Pre Alpha 0.1\Assets\Screen Fader Pack\Demo\Demo - Object Fading\DemoObjectFadingGUI.cs 198 Active
Warning CS0618 'TextEditor.content' is obsolete: 'Please use 'text' instead of 'content'' Go For Launch Pre Alpha 0.1.CSharp C:\Users\Joe\Documents\Go For Launch Pre Alpha 0.1\Assets\Screen Fader Pack\Demo\Demo - Object Fading\DemoObjectFadingGUI.cs 469 Active
Warning CS0618 'TextEditor.content' is obsolete: 'Please use 'text' instead of 'content'' Go For Launch Pre Alpha 0.1.CSharp C:\Users\Joe\Documents\Go For Launch Pre Alpha 0.1\Assets\Screen Fader Pack\Demo\Demo - Object Fading\DemoObjectFadingGUI.cs 477 Active
Warning CS0618 'TextEditor.content' is obsolete: 'Please use 'text' instead of 'content'' Go For Launch Pre Alpha 0.1.CSharp C:\Users\Joe\Documents\Go For Launch Pre Alpha 0.1\Assets\Screen Fader Pack\Demo\Demo - Object Fading\DemoObjectFadingGUI.cs 480 Active
Warning CS0618 'Screen.lockCursor' is obsolete: 'Property lockCursor has been deprecated. Use Cursor.lockState and Cursor.visible instead.' Go For Launch Pre Alpha 0.1.CSharp C:\Users\Joe\Documents\Go For Launch Pre Alpha 0.1\Assets\Spaceflight Controls\Scripts\CustomPointer.cs 48 Active
Warning CS0618 'Screen.lockCursor' is obsolete: 'Property lockCursor has been deprecated. Use Cursor.lockState and Cursor.visible instead.' Go For Launch Pre Alpha 0.1.CSharp C:\Users\Joe\Documents\Go For Launch Pre Alpha 0.1\Assets\Spaceflight Controls\Scripts\DemoUI.cs 27 Active
Warning CS0618 'Screen.lockCursor' is obsolete: 'Property lockCursor has been deprecated. Use Cursor.lockState and Cursor.visible instead.' Go For Launch Pre Alpha 0.1.CSharp C:\Users\Joe\Documents\Go For Launch Pre Alpha 0.1\Assets\Spaceflight Controls\Scripts\DemoUI.cs 29 Active
Warning CS0618 'Application.loadedLevel' is obsolete: 'Use SceneManager to determine what scenes have been loaded' Go For Launch Pre Alpha 0.1.CSharp C:\Users\Joe\Documents\Go For Launch Pre Alpha 0.1\Assets\Spaceflight Controls\Scripts\DemoUI.cs 45 Active
Warning CS0618 'Application.LoadLevel(int)' is obsolete: 'Use SceneManager.LoadScene' Go For Launch Pre Alpha 0.1.CSharp C:\Users\Joe\Documents\Go For Launch Pre Alpha 0.1\Assets\Spaceflight Controls\Scripts\DemoUI.cs 63 Active
Warning CS0618 'Application.LoadLevel(int)' is obsolete: 'Use SceneManager.LoadScene' Go For Launch Pre Alpha 0.1.CSharp C:\Users\Joe\Documents\Go For Launch Pre Alpha 0.1\Assets\Spaceflight Controls\Scripts\DemoUI.cs 66 Active
Warning CS0618 'Application.LoadLevel(int)' is obsolete: 'Use SceneManager.LoadScene' Go For Launch Pre Alpha 0.1.CSharp C:\Users\Joe\Documents\Go For Launch Pre Alpha 0.1\Assets\Spaceflight Controls\Scripts\DemoUI.cs 69 Active
Warning CS0618 'Application.LoadLevel(int)' is obsolete: 'Use SceneManager.LoadScene' Go For Launch Pre Alpha 0.1.CSharp C:\Users\Joe\Documents\Go For Launch Pre Alpha 0.1\Assets\Spaceflight Controls\Scripts\DemoUI.cs 72 Active
Warning CS0618 'Application.loadedLevel' is obsolete: 'Use SceneManager to determine what scenes have been loaded' Go For Launch Pre Alpha 0.1.CSharp C:\Users\Joe\Documents\Go For Launch Pre Alpha 0.1\Assets\Spaceflight Controls\Scripts\DemoUI.cs 81 Active
Warning CS0618 'Application.LoadLevelAsync(string)' is obsolete: 'Use SceneManager.LoadSceneAsync' Go For Launch Pre Alpha 0.1.CSharp C:\Users\Joe\Documents\Go For Launch Pre Alpha 0.1\Assets\Tales From The Rift\Splash\SplashController.cs 26 Active
Warning CS0618 'EditorApplication.currentScene' is obsolete: 'Use EditorSceneManager to see which scenes are currently loaded' Go For Launch Pre Alpha 0.1.CSharp.Editor C:\Users\Joe\Documents\Go For Launch Pre Alpha 0.1\Assets\CameraPath3\Editor\CameraPathEditorInspectorGUI.cs 1738 Active

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15318
  • Official Playmaker Support
    • View Profile
Re: Get/Set Angular Velocity
« Reply #9 on: June 06, 2016, 12:06:51 AM »
Hi,

 Please run the Unity Api updater, it will fix this.

 I have this thread as a pending todo for some weeks now... Time to make it happen... I'll clean it up and put it on the ecosystem asap.


Bye,

 Jean

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15318
  • Official Playmaker Support
    • View Profile
Re: Get/Set Angular Velocity
« Reply #10 on: June 06, 2016, 01:54:42 AM »
Hi,

 ok, they are both up on the Ecosystem now, let me know how it goes on your end.

 Bye,

 Jean

Mulbin

  • Junior Playmaker
  • **
  • Posts: 84
    • View Profile
Re: Get/Set Angular Velocity
« Reply #11 on: June 13, 2016, 08:50:47 AM »
Great thanks!  All working as it should