如果我的角色与某个对象相撞,我希望摄像机位置y向下移动一点。我试图这样编程:
N
但是它的行为不像我想要的那样。你们可以帮我吗?
当我的角色与一个物体碰撞并执行将移动我的相机的功能时,这是我相机的理想位置:
答案 0 :(得分:1)
问题是这行代码仅被调用一次(当您的角色碰撞时),并在到达所需目的地之前结束
Vector3 origPOS = new Vector3(x, (Mathf.Lerp(y, -3, 3 * Time.deltaTime)), 0)
您必须不断地忍住,直到达到这样的理想位置
float t = 0.0f;
while ( t<1.0f )
{
float x = Camera.main.transform.position.x;
float y = Camera.main.transform.position.y;
t += Time.deltaTime;
Vector3 origPOS = new Vector3(x, (Mathf.Lerp(y, -3, 3 * Time.deltaTime),t), 0)
Camera.main.transform.position = origPOS;
}
答案 1 :(得分:1)
在碰撞移动相机时,您可以启动Coroutine:
void OnTriggerEnter2D(Collider other)
{
StartCoroutine(MoveCamera());
}
IEnumerator MoveCamera()
{
float t = 0.0f;
Vector3 originPosition = Camera.main.transform.position;
Vector3 endPosition = new Vector3(originPosition.x, originPosition.y - 3, originPosition.z);
while ( t < 1.0f )
{
Camera.main.transform.position = Vector3.Lerp(originPosition, endPosition, t);
t += Time.deltaTime * CameraSpeed; // CameraSpeed is the speed of your camera.
yield return null; // Don't forget this line !
}
}