现在,我在这方面遇到了很多问题,我一直在使用他们作为2D资源包的一部分提供的基本Unity代码,可以如下所示:使用UnityEngine;使用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;
public float yPosRestriction = -1;
float offsetZ;
Vector3 lastTargetPosition;
Vector3 currentVelocity;
Vector3 lookAheadPos;
float nextTimeToSearch = 0;
// 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 Update () {
if (target == null) {
FindPlayer ();
return;
}
// 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);
newPos = new Vector3 (newPos.x, Mathf.Clamp (newPos.y, yPosRestriction, Mathf.Infinity), newPos.z);
transform.position = newPos;
lastTargetPosition = target.position;
}
void FindPlayer () {
if (nextTimeToSearch <= Time.time) {
GameObject searchResult = GameObject.FindGameObjectWithTag ("Player");
if (searchResult != null)
target = searchResult.transform;
nextTimeToSearch = Time.time + 0.5f;
}
}
}我一直有这个问题的主要原因之一是因为我对Unity还很陌生,而且只接触过UnityScript,但我的主要问题是,随着我游戏中的火箭速度增加,相机开始卡顿,我感觉这与阻尼有关?
发布于 2015-04-13 23:00:15
完全没有必要在update()中找到播放器(target)。我假设播放器后面的GameObject将保持不变,所以在Start()方法中找到播放器一次。
我不确定这是否是你的问题的原因。您可以通过将FindPlayer()中0.5f的绝对值设置为0.02f或其他值来临时尝试。这将更频繁地更新目标。如果这有帮助,那是因为你每秒只更新两次真实的目标位置。
发布于 2015-04-14 07:48:10
如果您没有定义摄影机脚本和目标移动发生的顺序,您的摄影机可能是动作后面的帧,这会导致卡顿。
更改您的相机脚本为:LateUpdate() -如果您是自己移动目标。FixedUpdate() -如果目标是被物理移动的刚体。
https://stackoverflow.com/questions/29605581
复制相似问题