Unity 5——编辑camera2DFollow脚本
Unity 5 - edit the camera2DFollow script
我使用的是 Unity 5 附带的标准 camera2DFollow
脚本。但是我对相机的位置有疑问。我旋转了我的主摄像头,现在看起来像这样。
你看到我的播放器在屏幕顶部而不是中间。
对于没有 C# 的人来说,这是默认脚本。
using System;
using UnityEngine;
namespace UnityStandardAssets._2D
{
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;
private float m_OffsetZ;
private Vector3 m_LastTargetPosition;
private Vector3 m_CurrentVelocity;
private Vector3 m_LookAheadPos;
// Use this for initialization
private void Start()
{
m_LastTargetPosition = target.position;
m_OffsetZ = (transform.position - target.position).z;
transform.parent = null;
}
// Update is called once per frame
private void Update()
{
// only update lookahead pos if accelerating or changed direction
float xMoveDelta = (target.position - m_LastTargetPosition).x;
bool updateLookAheadTarget = Mathf.Abs(xMoveDelta) > lookAheadMoveThreshold;
if (updateLookAheadTarget)
{
m_LookAheadPos = lookAheadFactor*Vector3.right*Mathf.Sign(xMoveDelta);
}
else
{
m_LookAheadPos = Vector3.MoveTowards(m_LookAheadPos, Vector3.zero, Time.deltaTime*lookAheadReturnSpeed);
}
Vector3 aheadTargetPos = target.position + m_LookAheadPos + Vector3.forward*m_OffsetZ;
Vector3 newPos = Vector3.SmoothDamp(transform.position, aheadTargetPos, ref m_CurrentVelocity, damping);
transform.position = newPos;
m_LastTargetPosition = target.position;
}
}
}
我想将 Y 更改为当前位置的 +3。所以如果我的相机在 Y 2 上而不是在 Y 5 上。(这使得玩家在中间而不是在顶部)。
感谢您的帮助!
您可以通过在每帧末尾的相机位置添加 3 来实现此目的,但我建议不要这样做。
我会做的是创建一个空对象,将其命名为 "PlayerCameraCenter" 并使玩家成为该对象的父对象;然后将相机中心放置在相对于玩家的任意位置,例如 y = 3,并使相机跟随此对象而不是玩家。
这样您就可以通过编辑器轻松更改相机的位置,而无需修改代码。
我使用的是 Unity 5 附带的标准 camera2DFollow
脚本。但是我对相机的位置有疑问。我旋转了我的主摄像头,现在看起来像这样。
你看到我的播放器在屏幕顶部而不是中间。
对于没有 C# 的人来说,这是默认脚本。
using System;
using UnityEngine;
namespace UnityStandardAssets._2D
{
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;
private float m_OffsetZ;
private Vector3 m_LastTargetPosition;
private Vector3 m_CurrentVelocity;
private Vector3 m_LookAheadPos;
// Use this for initialization
private void Start()
{
m_LastTargetPosition = target.position;
m_OffsetZ = (transform.position - target.position).z;
transform.parent = null;
}
// Update is called once per frame
private void Update()
{
// only update lookahead pos if accelerating or changed direction
float xMoveDelta = (target.position - m_LastTargetPosition).x;
bool updateLookAheadTarget = Mathf.Abs(xMoveDelta) > lookAheadMoveThreshold;
if (updateLookAheadTarget)
{
m_LookAheadPos = lookAheadFactor*Vector3.right*Mathf.Sign(xMoveDelta);
}
else
{
m_LookAheadPos = Vector3.MoveTowards(m_LookAheadPos, Vector3.zero, Time.deltaTime*lookAheadReturnSpeed);
}
Vector3 aheadTargetPos = target.position + m_LookAheadPos + Vector3.forward*m_OffsetZ;
Vector3 newPos = Vector3.SmoothDamp(transform.position, aheadTargetPos, ref m_CurrentVelocity, damping);
transform.position = newPos;
m_LastTargetPosition = target.position;
}
}
}
我想将 Y 更改为当前位置的 +3。所以如果我的相机在 Y 2 上而不是在 Y 5 上。(这使得玩家在中间而不是在顶部)。
感谢您的帮助!
您可以通过在每帧末尾的相机位置添加 3 来实现此目的,但我建议不要这样做。
我会做的是创建一个空对象,将其命名为 "PlayerCameraCenter" 并使玩家成为该对象的父对象;然后将相机中心放置在相对于玩家的任意位置,例如 y = 3,并使相机跟随此对象而不是玩家。
这样您就可以通过编辑器轻松更改相机的位置,而无需修改代码。