Hi,
Can someone help me out. I'm using platform 2d camera and It's jittery every few seconds, and I was wondering what the correct way on chaning the update in the script?
Thanks.
// (c) Copyright HutongGames, LLC 2010-2013. All rights reserved.
using UnityEngine;
namespace HutongGames.PlayMaker.Actions
{
[ActionCategory(ActionCategory.Transform)]
[Tooltip("Action for 2D Platform Camera with Dampening, based on Smooth Follow Action.")]
public class Platform2dCameraV2 : FsmStateAction
{
[RequiredField]
[Tooltip("The game object to control. E.g. The camera.")]
public FsmOwnerDefault gameObject;
[Tooltip("The GameObject to follow.")]
public FsmGameObject targetObject;
[RequiredField]
[Tooltip("The distance in the x-z plane to the target.")]
public FsmFloat distance;
[RequiredField]
[Tooltip("The height we want the camera to be above the target")]
public FsmFloat height;
[RequiredField]
[Tooltip("How much to dampen height movement.")]
public FsmFloat heightDamping;
[RequiredField]
[Tooltip("How much to horizontal offset.")]
public FsmFloat sideOffset;
[RequiredField]
[Tooltip("How much to dampen side movement.")]
public FsmFloat sideDamping;
[RequiredField]
[Tooltip("How much to dampen rotation changes.")]
public FsmFloat rotationDamping;
[Tooltip("Ignore Y position.")]
public bool ignoreHeight;
// Cache for performance
private GameObject cachedObect;
private Transform myTransform;
private Transform targetTransform;
public override void Reset()
{
gameObject = null;
targetObject = null;
distance = 10f;
height = 5f;
heightDamping = 2f;
sideOffset = 0f;
sideDamping = 2f;
rotationDamping = 0f;
ignoreHeight = false;
}
public override void OnUpdate()
{
if (targetObject.Value == null)
{
return;
}
var go = Fsm.GetOwnerDefaultTarget(gameObject);
if (go == null)
{
return;
}
if (cachedObect != go)
{
cachedObect = go;
myTransform = go.transform;
targetTransform = targetObject.Value.transform;
}
// Calculate the current rotation angles
var wantedRotationAngle = targetTransform.eulerAngles.y;
var wantedHeight = targetTransform.position.y + height.Value;
var wantedVel = targetTransform.position.x + sideOffset.Value;
var currentRotationAngle = myTransform.eulerAngles.y;
var currentHeight = myTransform.position.y;
var currentVel = myTransform.position.x;
// Damp the rotation around the y-axis
currentRotationAngle = Mathf.LerpAngle(currentRotationAngle, wantedRotationAngle, rotationDamping.Value * Time.deltaTime);
// Damp the height
if (ignoreHeight)
{
currentHeight = myTransform.position.y;
} else {
currentHeight = Mathf.Lerp(currentHeight, wantedHeight, heightDamping.Value * Time.deltaTime);
}
// Convert the angle into a rotation
var currentRotation = Quaternion.Euler(0, currentRotationAngle, 0);
// Set the position of the camera on the x-z plane to:
// distance meters behind the target
myTransform.position = targetTransform.position;
myTransform.position -= currentRotation * Vector3.forward * distance.Value;
currentVel = Mathf.Lerp(currentVel, wantedVel, sideDamping.Value * Time.deltaTime);
// Set the height of the camera
myTransform.position = new Vector3(currentVel, currentHeight, myTransform.position.z);
// Always look at the target
//myTransform.LookAt(targetTransform);
}
}
}