This is my customized mouselook, specifically coded for rotating camera.
Someone might notice that if an object got translated before or after applying mouselook,
then the Rotation will got a sharp reset.
Instead of setting the value of EulerAngle directly, this one simply do transform.rotate(),
So it will not do a reset jump like direct value setting.
This one will not do rotationZ,
Have no error warning if Rotation limit is none.
Will only limit Vertical orientation.
Could act funny if Min and Max is beyond -180, +180, Try it at your own risk.
----------------------------------------------
// (c) Copyright HutongGames, LLC 2010-2011. All rights reserved.
using UnityEngine;
namespace HutongGames.PlayMaker.Actions
{
/// <summary>
/// Action version of Unity's builtin MouseLook behaviour.
/// TODO: Expose invert Y option.
/// </summary>
[ActionCategory(ActionCategory.Input)]
[Tooltip("Rotates a GameObject based on mouse movement. Won't reset rotation when clicked like mouseLook.")]
public class MouseLookZennethCustom : FsmStateAction
{
[RequiredField]
[Tooltip("The GameObject to rotate.")]
public FsmOwnerDefault gameObject;
[HasFloatSlider(-360, 360)]
public FsmFloat minVertical;
[HasFloatSlider(-360, 360)]
public FsmFloat maxVertical;
[Tooltip("Repeat every frame.")]
public bool everyFrame;
float rotationX;
float rotationY;
float offset = 0.0f;
float divider = 1.2f;
float currentX = 0.0f;
public override void Reset()
{
gameObject = null;
minVertical = -60.0f;
maxVertical = 60.0f;
everyFrame = true;
}
public override void OnEnter()
{
var go = Fsm.GetOwnerDefaultTarget(gameObject);
if (go == null)
{
Finish();
return;
}
// Make the rigid body not change rotation
// TODO: Original Unity script had this. Expose as option?
if (go.rigidbody)
{
go.rigidbody.freezeRotation = true;
}
DoMouseLook();
if (!everyFrame)
{
Finish();
}
}
public override void OnUpdate()
{
DoMouseLook();
}
void DoMouseLook()
{
var go = Fsm.GetOwnerDefaultTarget(gameObject);
if (go == null)
{
return;
}
var transform = go.transform;
//Rotate 1st
transform.Rotate(new Vector3(GetYRotation(), GetXRotation(), 0));
//Clamp value if vertical too high or low
currentX = ClampAngle(transform.localEulerAngles.x,minVertical,maxVertical);
//Debug.Log(currentX + "localEulerX = " + transform.localEulerAngles.x + " || EulerX = " + transform.eulerAngles.x);
transform.localEulerAngles = new Vector3(currentX, transform.localEulerAngles.y, 0);
}
float GetXRotation()
{
offset = Input.GetAxis("Mouse X");
return offset/divider;
}
float GetYRotation()
{
offset = -Input.GetAxis("Mouse Y");
return offset/divider;
}
// Clamp function that respects IsNone
static float ClampAngle(float angle, FsmFloat min, FsmFloat max)
{
//max onward to 180
if (!min.IsNone && ( angle < 180.0f && angle > max.Value ))
{
angle = max.Value;
}
//need to convert since no negative number
float newMin = min.Value + 360.0f;
if (!max.IsNone && ( angle > 180.0f && angle < newMin ))
{
angle = newMin;
}
return angle;
}
}
}