【发布时间】:2015-11-09 14:16:46
【问题描述】:
我正在做一个 2D 平台游戏,玩家只能攀爬(y 轴)。 我正在尝试做的是,当我接近顶部边缘时,我的相机会上升一点,这样我就可以看到更多的关卡。
我有这个,但它不工作:
Public Transform player;
Void Start() { }
Void Update()
{
if (player.transform.position > Screen.height - 50)
{
transform.position.y += speed * Time.deltaTime;
}
}
Other Way is like this 但不工作不止一次,我可能需要设置 move = false;但不知道如何不打断 Lerp:
float moveTime = 1f; // In seconds
float moveTimer = 0.0f; // Used for keeping track of time
bool moving = false; // Flag letting us know if we're moving
public float heightChange = 7.0f; // This is the delta
// These will be assigned when a collision occurs
Vector3 target; // our target position
Vector3 startPos; // our starting position
void Start()
{
}
void Update()
{
// If we're currently moving and the movement hasn't finished
if (moving && moveTimer < moveTime)
{
// Accumulate the frame time, making the timer tick up
moveTimer += Time.deltaTime;
// calculate our ratio ("t")
float t = moveTimer / moveTime;
transform.position = Vector3.Lerp(startPos, target, t);
}
else
{
// We either haven't started moving, or have finished moving
}
}
void OnTriggerEnter2D(Collider2D other)
{
if (!moving)
{
// We set the target to be ten units above our current position
target = transform.position + Vector3.up * heightChange;
// And save our start position (because our actual position will be changing)
startPos = transform.position;
// Set the flag so that the movement starts
moving = true;
}
}
}
【问题讨论】:
-
你只检查
player.transform.position,应该是player.transform.position.y > Screen.height - 50 -
Public和Void应为小写。 -
仍然无法正常工作,是的,我知道,但我只是在这里手工制作的 =P