Playmaker Forum

PlayMaker Updates & Downloads => Share New Actions => Topic started by: Zenneth on June 07, 2012, 05:41:03 PM

Title: Customized mouse look, no angle reset if you click after object got translated
Post by: Zenneth on June 07, 2012, 05:41:03 PM
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.

----------------------------------------------

Code: [Select]
// (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;
}
}
}
Title: Re: Customized mouse look, no angle reset if you click after object got translated
Post by: jeanfabre on June 08, 2012, 06:49:43 AM
Hi,

 cool!

 As for your poll, I think it should not. Instead I think authors should provide their set of actions on the asset store. PLaymaker can not include everything, it's just overwhelming. I am myself thinking of making a "essential pack" of all the custom action I made so far for a symbolic few bucks, but never had the time to get around doing this. The very good side effect of distributing on the asset store is that the user doesn't have to search the forum for all them actions, and benefit from update automatically and conveniently.

 Bye,

 Jean
Title: Re: Customized mouse look, no angle reset if you click after object got translated
Post by: doppelmonster on February 24, 2014, 12:11:23 PM
That was exactly what i needed. Works great! I added sensitivity controls.
Title: Re: Customized mouse look, no angle reset if you click after object got translated
Post by: MAX_POWER on December 06, 2015, 12:31:15 PM
Hello, this mouselook makes it possible to do excatly what I've been trying to do.
I can play character animation and the player gets back to control the character where the animation left...but.. The only problem is that if I use this in the character itself it will rotate from the center making the basic vertical bug. If I apply another mouselookcustom to the camera and rotate the camera vertically and character horizontally the "controls" flip sometimes so that the character will walk backwards when pressing forward. So basically the camera turns around to point to otherside of the character. Any ideas how to fix it? Or is there another way to do what the script is doing with basic mouselook. thanks