【发布时间】:2022-01-22 12:38:43
【问题描述】:
在 Unity 中,我有一个相机作为 2D 游戏对象的子对象(跟随它)。有一个 IF 语句可以让我通过按住一个键向前移动相机。放手后,我需要一个代码将相机返回给游戏对象。谢谢你的帮助。
public class camera : MonoBehaviour
{
public float panspeed = 30f;
public float panBorderThickness = 30f;
public GameObject ship1;
private Vector3 offset;
void Update()
{
if (Input.GetKey("f"))
{
Vector3 pos = transform.position;
if (Input.mousePosition.y >= Screen.height - panBorderThickness)
{
pos.y += panspeed * Time.deltaTime;
}
if (Input.mousePosition.y <= panBorderThickness)
{
pos.y -= panspeed * Time.deltaTime;
}
if (Input.mousePosition.x >= Screen.width - panBorderThickness)
{
pos.x += panspeed * Time.deltaTime;
}
if (Input.mousePosition.x <= panBorderThickness)
{
pos.x -= panspeed * Time.deltaTime;
}
transform.position = pos;
}
//something to return the camera back when i let go of F key
}
}
【问题讨论】:
-
这行得通吗? hastebin.com/takefujisu.hs
-
如果是的,我将添加作为答案,我刚刚编写了伪代码 rn 没有编辑器
-
好吧,当你放手时,你需要使用相同的过程将其移回。