playMaker

Author Topic: RotateAround  (Read 23666 times)

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
RotateAround
« on: February 09, 2012, 03:00:38 AM »
Hi,

 Following a post:

 Please find a very important addition. being able to use the rotateAround function.

 You can specific a gameObject to rotate around, and also specify that the axis are defined locally, ( so the gameObject will rotate around this target position AND axis).

Also, If you specific a gameObject to rotate around, the position variable will be use as an offset.

It's now also available on the Ecosystem ( via snipt)


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

using UnityEngine;

namespace HutongGames.PlayMaker.Actions
{
[ActionCategory(ActionCategory.Transform)]
[Tooltip("Rotates Around a Game Object.")]
public class RotateAround : FsmStateAction
{

[RequiredField]
public FsmOwnerDefault gameObject;

[ActionSection("Rotation position")]
[Tooltip("Rotate around this GameObject.")]
public FsmGameObject aroundGameObject;

[Tooltip("Rotate around this point. If 'aroundGameObject' defined, will offset by 'rotationPoint'")]
public FsmVector3 rotationPoint;

[ActionSection("Rotation axis")]
[Tooltip("Rotate around this axis.")]
public FsmVector3 rotationAxis;

[Tooltip("If 'aroundGameObject' defined and 'useAroundGameObjectAxisSpace' TRUE, 'rotationAxis' will be defined in 'aroundGameObject' local space.")]
public FsmBool useAroundGameObjectAxisSpace;

[ActionSection("Angle")]
[Tooltip("Amount to rotate in degrees.")]
public FsmFloat angle;

[Tooltip("Rotate over one second")]
public bool perSecond;

[ActionSection(" ")]
public bool everyFrame;

public override void Reset()
{
gameObject = null;
aroundGameObject = null;
angle = null;

useAroundGameObjectAxisSpace = false;

perSecond = false;
everyFrame = true;
}

public override void OnEnter()
{
DoRotateAround();

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

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

void DoRotateAround()
{
var go = Fsm.GetOwnerDefaultTarget(gameObject);

Vector3 _rotationPoint = rotationPoint.Value;
Vector3 _axis = rotationAxis.Value;

GameObject aroundgo = aroundGameObject.Value;
if (aroundgo!=null)
{
_rotationPoint += aroundgo.transform.position;


}

if (useAroundGameObjectAxisSpace.Value)
{
_axis = aroundgo.transform.TransformDirection(_axis);
}

float _angle = angle.Value;

if (perSecond)
{
_angle *= Time.deltaTime;
}

go.transform.RotateAround(_rotationPoint,_axis,_angle);

}

public override string ErrorCheck()
{
if (useAroundGameObjectAxisSpace.Value && aroundGameObject.Value==null)
{
return "'useAroundGameObjectAxisSpace' is only effective is 'aroundGameObject' defined";
}

return "";
}

}
}

 Any suggestions or comments to improve it welcome as always.

Bye,

 Jean
« Last Edit: June 03, 2015, 03:56:47 AM by jeanfabre »

Andrew.Lukasik

  • Full Member
  • ***
  • Posts: 134
    • my twitter @andrewlukasik
Re: RotateAround
« Reply #1 on: February 13, 2012, 08:42:38 AM »
Yes! I just needed that :D thanks, very usefull for cinematic cameras

cel

  • Full Member
  • ***
  • Posts: 132
Re: RotateAround
« Reply #2 on: February 17, 2012, 08:37:43 AM »
I'm trying to get my object to increment its rotation around another object each time a key is pressed, can't get it to work... coud someone post a screen on how to achieve this? Thanks in advance

bigzer

  • Playmaker Newbie
  • *
  • Posts: 11
  • 1.3
Re: RotateAround
« Reply #3 on: March 21, 2012, 04:36:20 AM »
Great Action Thanks :)

A small improvement whould be to have checkboxes for the axis instead of X Y Z forms.
It whould make this action more clear to understand.

Anyway great work ^^
« Last Edit: March 21, 2012, 04:42:22 AM by bigzer »

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: RotateAround
« Reply #4 on: March 21, 2012, 07:15:14 AM »
Hi,

 Well, doing so would constraint into orthonormal rotation, while offering the vector, means we can rotate around any direction in space.

But yes, less intuitive than check box I give you that...


 Bye,

 Jean

4s4

  • Playmaker Newbie
  • *
  • Posts: 34
Re: RotateAround
« Reply #5 on: April 20, 2012, 10:16:04 AM »
thank you for this! my own version of this wasn't nearly as useful :)

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: RotateAround
« Reply #6 on: April 22, 2012, 05:37:59 PM »
hi 4s4,

 We all have to start somewhere, and I am sure this action can also be improved anyway! I hope you have learned things, and be able to apply it to new actions you will build, don't forget to share them back, this will be greatly appreciated!

Bye,

 Jean

Dev_Sebas

  • 1.2 Beta
  • Sr. Member
  • *
  • Posts: 398
    • Blog
Re: RotateAround
« Reply #7 on: June 27, 2012, 01:58:37 PM »
Lovely thx a lot Jean ;D :) :-*
Bye

sinman

  • Playmaker Newbie
  • *
  • Posts: 14
Re: RotateAround
« Reply #8 on: August 23, 2012, 11:14:29 PM »
Hi Jean and co,

I wanted to use this function and downloaded the CS script by Jean. I've dragged the file onto my project folder, then dragged this script onto a game object. However, I get the following message:

Quote
Can't add script behaviour RotateAround. The scripts file name does not match the name of the class defined in the script!

How can I go about implementing this function?

I have
- Unity 3.5.5f3
- Playmaker 1.4.3
- OSX

Thanks.

Groo Gadgets

  • Beta Group
  • Full Member
  • *
  • Posts: 175
Re: RotateAround
« Reply #9 on: August 24, 2012, 12:00:30 AM »
Hey Sinman,

I haven't used this action yet but i'm pretty sure you have to drag the script into your /PlayMaker/Actions/ folder for it to become active as an action via PlayMaker.

Once you've done that it should be accessible under the Transform action category.

Hope this helps  :)

Simon

sinman

  • Playmaker Newbie
  • *
  • Posts: 14
Re: RotateAround
« Reply #10 on: August 24, 2012, 05:37:17 AM »
Hi Avrigus

Thanks for the input. The idea for looking into the action browser has totally eluded me. Works fine now. Thanks!

Sinman

amaranth

  • Full Member
  • ***
  • Posts: 172
Re: RotateAround
« Reply #11 on: September 28, 2012, 04:31:10 PM »
I'm trying to use this, but I think I'm confused. Maybe I am trying to use this for the wrong thing? Here is what I'm trying to do:

World is 2D. Sun only moves on X & Y axis. Sun doesn't rotate, but moves in a circle around the core of the planet.

Is this something RotateAround does? Or should I be using another action? I'm avoiding iTween since I heard it's bad for mobile.

Thank you for any help.

amaranth

  • Full Member
  • ***
  • Posts: 172
Re: RotateAround
« Reply #12 on: September 28, 2012, 05:29:14 PM »
Never mind, I found this. Didn't realize how simple it was.

http://answers.unity3d.com/questions/38932/Circular-movement.html

louismg

  • Playmaker Newbie
  • *
  • Posts: 13
Re: RotateAround
« Reply #13 on: November 28, 2013, 11:22:52 PM »
I humbly submit an enhanced version.

If you use the "every frame" mode, you can now specify, in "Stop Angle", a number of degrees after which the action will stop.  If you leave zero, it will rotate around forever.

That is very useful if you want to string together a series of moves.

Beware:

1) I am an old super rusty programmer;
2) I have no experience at all for 3D stuff;
3) I have not studied how to program PlayMaker actions at all, I just "copied" what I saw in this action and tried to imitate the form and style as closely as I could.

So use with caution… For example: I suspect this would break down if the rotation speed was super fast because my check to stop the rotation is very primitive. 

Maybe Jean or another seasoned programmer would want to check it. It's a very simple mod, it should take but a few seconds to validate.

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

using UnityEngine;

namespace HutongGames.PlayMaker.Actions
{
[ActionCategory(ActionCategory.Transform)]
[Tooltip("Rotates Around a Game Object.")]
public class RotateAround : FsmStateAction
{

[RequiredField]
public FsmOwnerDefault gameObject;

[ActionSection("Rotation position")]
[Tooltip("Rotate around this GameObject.")]
public FsmGameObject aroundGameObject;

[Tooltip("Rotate around this point. If 'aroundGameObject' defined, will offset by 'rotationPoint'")]
public FsmVector3 rotationPoint;

[ActionSection("Rotation axis")]
[Tooltip("Rotate around this axis.")]
public FsmVector3 rotationAxis;

[Tooltip("If 'aroundGameObject' defined and 'useAroundGameObjectAxisSpace' TRUE, 'rotationAxis' will be defined in 'aroundGameObject' local space.")]
public FsmBool useAroundGameObjectAxisSpace;

[ActionSection("Angle")]
[Tooltip("Amount to rotate in degrees.")]
public FsmFloat angle;

[Tooltip("Rotate over one second")]
public bool perSecond;

[ActionSection("Animation")]
public bool everyFrame;
[Tooltip("Amount to rotate in degrees before stopping animation. Zero rotates forever.")]
public FsmFloat stopAngle;
FsmFloat rotatedAngle = 0f;

public override void Reset()
{
gameObject = null;
aroundGameObject = null;
stopAngle = null;
angle = null;
rotatedAngle = 0f;

useAroundGameObjectAxisSpace = false;

perSecond = false;
everyFrame = true;
}

public override void OnEnter()
{
rotatedAngle.Value = 0;

DoRotateAround();

if(!everyFrame)
{
Finish();
}
else if (stopAngle.Value != 0f)
{
if (rotatedAngle.Value >= stopAngle.Value)
{
Finish();
}
}
}

public override void OnUpdate()
{
DoRotateAround();

if (stopAngle.Value != 0f)
{
if (rotatedAngle.Value >= stopAngle.Value)
{
Finish();
}
}
}

void DoRotateAround()
{
var go = Fsm.GetOwnerDefaultTarget(gameObject);

Vector3 _rotationPoint = rotationPoint.Value;
Vector3 _axis = rotationAxis.Value;

GameObject aroundgo = aroundGameObject.Value;
if (aroundgo!=null)
{
_rotationPoint += aroundgo.transform.position;


}

if (useAroundGameObjectAxisSpace.Value)
{
_axis = aroundgo.transform.TransformDirection(_axis);
}

float _angle = angle.Value;

if (perSecond)
{
_angle *= Time.deltaTime;
}

rotatedAngle.Value += _angle;

go.transform.RotateAround(_rotationPoint,_axis,_angle);

}

public override string ErrorCheck()
{
if (useAroundGameObjectAxisSpace.Value && aroundGameObject.Value==null)
{
return "'useAroundGameObjectAxisSpace' is only effective is 'aroundGameObject' defined";
}

return "";
}

}
}

Modified: already one bug squashed...
« Last Edit: November 28, 2013, 11:32:43 PM by louismg »

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: RotateAround
« Reply #14 on: November 29, 2013, 06:34:00 AM »
Hi,

 Excellent! I don't see anything wrong in the way you have implemented the angle stop check, I don't think I would have done it differently anyway!


Bye,

Jean