- Home /
Unity 2D Shaking Player With Camera Damping
Hi Desclaimer: sorry for my english,i'm italian. I have a problem with this script:
using UnityEngine;
using System.Collections;
public class Camera2DFollow : MonoBehaviour {
public Transform target;
public float damping = 1;
public float lookAheadFactor = 3;
public float lookAheadReturnSpeed = 0.5f;
public float lookAheadMoveThreshold = 0.1f;
float offsetZ;
Vector3 lastTargetPosition;
Vector3 currentVelocity;
Vector3 lookAheadPos;
// Use this for initialization
void Start () {
lastTargetPosition = target.position;
offsetZ = (transform.position - target.position).z;
transform.parent = null;
}
// Update is called once per frame
void LateUpdate () {
// only update lookahead pos if accelerating or changed direction
float xMoveDelta = (target.position - lastTargetPosition).x;
bool updateLookAheadTarget = Mathf.Abs(xMoveDelta) > lookAheadMoveThreshold;
if (updateLookAheadTarget) {
lookAheadPos = lookAheadFactor * Vector3.right * Mathf.Sign(xMoveDelta);
} else {
lookAheadPos = Vector3.MoveTowards(lookAheadPos, Vector3.zero, Time.deltaTime * lookAheadReturnSpeed);
}
Vector3 aheadTargetPos = target.position + lookAheadPos + Vector3.forward * offsetZ;
Vector3 newPos = Vector3.SmoothDamp(transform.position, aheadTargetPos, ref currentVelocity, damping);
transform.position = newPos;
lastTargetPosition = target.position;
}
}
The problem are this,when i add the script in the main camera if i set damping to 0 there are not problem but if damping is greater than 0 in play the player when it moves shaking. who is the problem? for see the problem download this build: http://www.mediafire.com/download/5au0znhqg1ra6t3/Super_Mario_v1.1.zip Thanks
Comment
Your answer
Follow this Question
Related Questions
Camera animations? 2 Answers
A node in a childnode? 1 Answer
Camera is moving strangely 2 Answers
How to export MMD model to Unity? 0 Answers