playMaker

Author Topic: Smooth Follow Camera 2d Jittery. Change LateUpdate? (Solved)  (Read 2768 times)

RC

  • Full Member
  • ***
  • Posts: 148
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.

Code: [Select]
// (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);
}

}
}
« Last Edit: June 20, 2015, 10:51:46 AM by rongconcrx »

RC

  • Full Member
  • ***
  • Posts: 148
Re: Smooth Follow Camera 2d Jittery. Change LateUpdate?
« Reply #1 on: June 19, 2015, 02:08:19 PM »
nvm, I don't know what I'm talking about.

I think I understand how update/lateupdate works.

Sorry for the novice question.

djaydino

  • Administrator
  • Hero Member
  • *****
  • Posts: 7616
    • jinxtergames
Re: Smooth Follow Camera 2d Jittery. Change LateUpdate?
« Reply #2 on: June 19, 2015, 05:08:40 PM »
hi,

no problem, any question you can ask (on topic ofc  :) )

Could you just add [SOLVED] to you topic pls.

dudebxl

  • Hero Member
  • *****
  • Posts: 602
Re: Smooth Follow Camera 2d Jittery. Change LateUpdate? (Solved)
« Reply #3 on: September 29, 2015, 03:34:48 PM »
Another action that maybe should be in ecosystem??