playMaker

Author Topic: CameraOrbit  (Read 5535 times)

giyomu

  • 1.2 Beta
  • Full Member
  • *
  • Posts: 108
    • blog
CameraOrbit
« on: July 05, 2011, 02:37:03 AM »
something handy to use for a simple camera orbit behavior

I did not test in all possible condition so it may get some issue..

Code: [Select]
using UnityEngine;

namespace HutongGames.PlayMaker.Actions
{
[ActionCategory(ActionCategory.Camera)]
[Tooltip("Orbit a camera from passed input value, ideally feed input value with device input. Performed every frame and per  second" +
"Set inputX or inputY to None if you want to keep these value inchanged")]
public class CameraOrbit : FsmStateAction
{
[RequiredField]
public FsmOwnerDefault gameObject;
[Tooltip("in landscape condition for iOS invert X and Y")]
public FsmFloat inputRotationX;
public FsmFloat inputRotationY;
public FsmFloat sensitivityX;
public FsmFloat sensitivityY;
public FsmBool clampX;
public FsmFloat xMin;
public FsmFloat xMax;
public FsmBool clampY;
public FsmFloat yMin;
public FsmFloat yMax;
[Tooltip("Smooth rotation using Slerp")]
public FsmBool smoothRotation;
[HasFloatSlider(1f,15)]
public FsmFloat smoothFactor;
[Tooltip("will offset our camera to keep it facing correctly from it primary set rotation, concern our Y angle")]
public FsmFloat rotationOffset;

Transform cachedTransform = null;
float x = 0f;
float y = 0f;
float usedX = 0f;
float usedY = 0f;

public override void Reset ()
{
gameObject = null;
inputRotationX = new FsmFloat {UseVariable = true};
inputRotationY = new FsmFloat {UseVariable = true};
sensitivityX = 5f;
sensitivityY = 5f;
clampX = false;
xMin = -40f;
xMax = 40f;
clampY = true;
yMin = -80f;
yMax = 80f;
smoothRotation = false;
smoothFactor = 1f;
rotationOffset = 0f;
}

public override void OnEnter ()
{
GameObject go = Fsm.GetOwnerDefaultTarget(gameObject);
if (go == null)
Finish();
                        else
      cachedTransform = go.transform;
}

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

public override void OnLateUpdate ()
{
DoOrbit();
}

void GetInput()
{
x += inputRotationX.Value * sensitivityX.Value;
y -= inputRotationY.Value * sensitivityY.Value;

// clamp our input
if(clampX.Value)
x = ClampAngle(x, xMin.Value, xMax.Value);

if(clampY.Value)
y = ClampAngle(y, yMin.Value, yMax.Value);
}

void DoOrbit()
{
// determine if we let value inchanged for x and y
if(inputRotationX.IsNone)
usedX = cachedTransform.rotation.eulerAngles.x;
else
usedX = x;

if(inputRotationY.IsNone)
usedY = cachedTransform.rotation.eulerAngles.y;
else
usedY = y;

// do our rotation stuff
if(!smoothRotation.Value)
cachedTransform.rotation = Quaternion.Euler(usedX, usedY + rotationOffset.Value, 0f);
else
{
var lastRotation = cachedTransform.rotation;
var rotation = Quaternion.Euler(usedX, usedY + rotationOffset.Value, 0f);
cachedTransform.rotation = Quaternion.Slerp(lastRotation, rotation, Time.deltaTime * smoothFactor.Value);
}
}

static float ClampAngle(float angle, float min, float max)
{
if(angle < -360f)
angle += 360f;
if(angle > 360f)
angle -= 360f;

return Mathf.Clamp(angle, min, max);
}
}
}
« Last Edit: July 05, 2011, 02:43:38 AM by giyomu »

Linusnorden

  • Playmaker Newbie
  • *
  • Posts: 4
Re: CameraOrbit
« Reply #1 on: July 09, 2011, 05:28:19 PM »
Great just what ive been looking for... I guess i need to use get mouse x and y and then save the var and then use it in this script? do you know if i need to
convert the x and y form the mouse before i can use it as rotation input?

I really want a smooth zoom in mouse wheel to is that a easy fix?

Thanks!

giyomu

  • 1.2 Beta
  • Full Member
  • *
  • Posts: 108
    • blog
Re: CameraOrbit
« Reply #2 on: July 09, 2011, 07:27:44 PM »
Hi,

yes just get your x and y input before and pass this in teh xy input of teh CameraOrbit Action.

for camera zoom you can either use another Fsm or then if you want do that in this script you need to check for the scroll wheel input, and handle the distance between your camera and target , so that was not really the goal of this script , so i didn't add this.

let me know if you are stuck with this maybe i can help :)

Linusnorden

  • Playmaker Newbie
  • *
  • Posts: 4
Re: CameraOrbit
« Reply #3 on: July 10, 2011, 04:27:43 AM »
Hi again

Well i used the get mouse x and y and the vars into this action but the camera just start spinning. I guess i need to convert the mouse input to work in the action?
Do you have a working example?

giyomu

  • 1.2 Beta
  • Full Member
  • *
  • Posts: 108
    • blog
Re: CameraOrbit
« Reply #4 on: July 10, 2011, 06:30:16 AM »
Ah well try use getaxis action and catch the "Mouse X" and same for Y, if you catch mouse screen position that will certainly spin yes

PolyMad

  • Hero Member
  • *****
  • Posts: 545
Re: CameraOrbit
« Reply #5 on: May 02, 2013, 11:02:36 AM »
I was trying this action but can't see how it works.
I tried to put the target object and change parameters but its behaviour is quite weird.

Then, there's the MouseOrbit script that applied to my camera works like a charm, but I need to set up a variable inside it from a FSM, is it possible?