Playmaker Forum

PlayMaker Updates & Downloads => Share New Actions => Topic started by: jeanfabre on February 09, 2012, 03:00:38 AM

Title: RotateAround
Post by: jeanfabre 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 (https://hutonggames.fogbugz.com/default.asp?W1181) ( via snipt (https://jeanfabre.snipt.net/rotatearound-8ed6caa8/))


 
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
Title: Re: RotateAround
Post by: Andrew.Lukasik on February 13, 2012, 08:42:38 AM
Yes! I just needed that :D thanks, very usefull for cinematic cameras
Title: Re: RotateAround
Post by: cel 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
Title: Re: RotateAround
Post by: bigzer 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 ^^
Title: Re: RotateAround
Post by: jeanfabre 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
Title: Re: RotateAround
Post by: 4s4 on April 20, 2012, 10:16:04 AM
thank you for this! my own version of this wasn't nearly as useful :)
Title: Re: RotateAround
Post by: jeanfabre 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
Title: Re: RotateAround
Post by: Dev_Sebas on June 27, 2012, 01:58:37 PM
Lovely thx a lot Jean ;D :) :-*
Bye
Title: Re: RotateAround
Post by: sinman 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.
Title: Re: RotateAround
Post by: Groo Gadgets 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
Title: Re: RotateAround
Post by: sinman 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
Title: Re: RotateAround
Post by: amaranth 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.
Title: Re: RotateAround
Post by: amaranth 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
Title: Re: RotateAround
Post by: louismg 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...
Title: Re: RotateAround
Post by: jeanfabre 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
Title: Re: RotateAround
Post by: Joe64 on February 01, 2014, 01:39:43 PM
Hi,

useful action and nice feature "Stop Angle"!
I've played around with it and I think I found an issue. When you use negative values for stopAngle in order to rotate counter clockwise then it will never stop.
The line
Code: [Select]
rotatedAngle.Value += _angle;should read
Code: [Select]
rotatedAngle.Value += Mathf.Abs(_angle);Or is there another way to rotate counter clockwise? At least this worked for me.

Also a small question: Sometimes there's no file to download for a new shared action. Am I supposed to copy the code and create a .cs file or is there another trick? (I'm new to this forum, as you can see  ;) ). Thanks.
Title: Re: RotateAround
Post by: jeanfabre on February 03, 2014, 04:59:16 AM
Hi,

 yes, copy the code snippet, create a c# file of the exact same name as the class defined in that code and paste it in there.

also, I always recommand that you create a special folder to host all your custom actions, like "PlayMaker Custom Actions", I go one more step as well, creating sub folders of the name of the category, so that all my custom actions are organized by categories in my project folder.


bye,

 Jean
Title: Re: RotateAround
Post by: dottim on June 08, 2014, 01:54:27 AM
hello,

Is there a way to alter the code slightly so I can make the rotation stop when timescale is set to 0? Right now when the game is paused the rotation continues :/

tia
Title: Re: RotateAround
Post by: Alex Chouls on June 08, 2014, 02:25:25 AM
You could multiply your rotation angle by the current time scale. Use Get Time Info to get Time Scale and then Float Multiply.
Title: Re: RotateAround
Post by: Sjones on June 13, 2014, 11:32:09 AM
Just by chance I have just needed to make this work in real time, thanks for the hint alex, just spent some time trying to figure this out!
Title: Re: RotateAround
Post by: wrongtarget on March 24, 2015, 10:02:55 AM
Couldn't scripts like this be added to the Ecosystem?  :)
Title: Re: RotateAround
Post by: Nog on June 01, 2015, 04:00:31 PM
Hello,

What does the Rotation Point do? Is it possible to create a rotation from which the GameObject will animate from like the Vector Rotation in the Itween Rotate action?

I am trying to get my camera to rotate around a character like a third person game. The Itween Rotate action is OK, but I wanted a bit more control over how and when the camera rotates.

Thanks,

Nog
Title: Re: RotateAround
Post by: jeanfabre on June 03, 2015, 03:57:54 AM
Couldn't scripts like this be added to the Ecosystem?  :)

Hi,

 True, it's now up. Simply bump on the actions you want to see promoted to the Ecosystem, there is just too much existing actions on the forum already to go through manually :)

Bye,

 Jean
Title: Re: RotateAround
Post by: jeanfabre on June 03, 2015, 04:00:50 AM
What does the Rotation Point do? Is it possible to create a rotation from which the GameObject will animate from like the Vector Rotation in the Itween Rotate action?


Hi

Rotation point is your "center" around which you are going to turn, ( your character in your case).

 Bye,

 Jean
Title: Re: RotateAround
Post by: Ouren on October 16, 2015, 08:28:50 PM
Hey Jean, the Script in the Ecosystem is not the updated one with the "Stop Angle" function.

The latest one can be found in this same thread here
http://hutonggames.com/playmakerforum/index.php?topic=1078.msg27003#msg27003
Title: Re: RotateAround
Post by: jeanfabre on October 27, 2015, 06:27:04 AM
Hi,

 Ok, done, redownload please.

Do you want to host it yourself? I used my snipt account, but you can create yours if you want and then you can update it.

 Bye,

 Jean