Hello , I am working on 2d game , and in one of my objects I need to limit the rotation done by Look At 2d .
// (c) Copyright HutongGames, LLC 2010-2014. All rights reserved.
// original action created by collidernyc: http://hutonggames.com/playmakerforum/index.php?topic=7075.msg37373#msg37373
using UnityEngine;
namespace HutongGames.PlayMaker.Actions
{
[ActionCategory(ActionCategory.Transform)]
[Tooltip("Rotates a 2d Game Object on it's z axis so its forward vector points at a 2d or 3d position.")]
public class ClampedLookAt2d : FsmStateAction
{
[RequiredField]
[Tooltip("The GameObject to rotate.")]
public FsmOwnerDefault gameObject;
[Tooltip("The 2d position to Look At.")]
public FsmVector2 vector2Target;
[Tooltip("The 3d position to Look At. If not set to none, will be added to the 2d target")]
public FsmVector3 vector3Target;
[Tooltip("Set the GameObject starting offset. In degrees. 0 if your object is facing right, 180 if facing left etc...")]
public FsmFloat rotationOffset;
[Title("Draw Debug Line")]
[Tooltip("Draw a debug line from the GameObject to the Target.")]
public FsmBool debug;
[Tooltip("Color to use for the debug line.")]
public FsmColor debugLineColor;
[Tooltip("Repeat every frame.")]
public bool everyFrame = true;
private GameObject go;
private Vector3 lookAtPos;
public override void Reset()
{
gameObject = null;
vector2Target = null;
vector3Target = new FsmVector3() {UseVariable=true};
debug = false;
debugLineColor = Color.green;
everyFrame = true;
}
public override void OnEnter()
{
DoLookAt();
if (!everyFrame)
{
Finish();
}
}
public override void OnUpdate()
{
DoLookAt();
}
void DoLookAt()
{
go = Fsm.GetOwnerDefaultTarget(gameObject);
if (go == null)
{
return;
}
Vector3 target = new Vector3(vector2Target.Value.x,vector2Target.Value.y,0f);
if (!vector3Target.IsNone)
{
target += vector3Target.Value;
}
Vector3 diff = target - go.transform.position;
diff.Normalize();
float rot_z = Mathf.Atan2(diff.y, diff.x) * Mathf.Rad2Deg;
go.transform.rotation = Quaternion.Euler(0f, 0f, rot_z - rotationOffset.Value);
if (debug.Value)
{
Debug.DrawLine(go.transform.position, target, debugLineColor.Value);
}
}
}
}
Could anyone help or hint another way to do things .
I am trying to rotate a gun mounted on 2d ship that rotates in Z axis .
But I want to limit guns rotation to some degree
I am using mouse position to rotate the gun.
thanks