gpt4 book ai didi

c# - Unity 2D - 放大 = ok,缩小 = borked

转载 作者:太空宇宙 更新时间:2023-11-03 23:38:57 24 4
gpt4 key购买 nike

我希望有人能提供帮助。我正在尝试创建一个小脚本来放大我的播放器并退出 - 切换。放大效果很好,但是当我尝试缩小时它不起作用,它卡住了。我创建了一个 bool 以确保它只在需要时运行代码,我想知道这是否是导致错误的原因。

using UnityEngine;
using System.Collections;

public class CameraZoom : MonoBehaviour
{

public float zoom = 10f;
public float normal = 3.471398f;
public float smooth = 5f;
private bool isZoomed = false;

public Camera cam;
public GameObject player;

// lock the camera settings
public float LockedX = 0f;
public float LockedY = 0f;
public float LockedZ = 0f;
private bool hasBeenZoomed = false;

Vector3 targetPos;

private Transform playerTransform;

// Use this for initialization
void Start()
{
targetPos = transform.position;
playerTransform = GameObject.FindGameObjectWithTag("Player").transform;
}

// Update is called once per frame
void Update()
{
if (Input.GetKeyDown("z")) { isZoomed = !isZoomed; }
if (isZoomed == true)
{
ZoomInToPlayer();
hasBeenZoomed = true;
}
else
{
if (hasBeenZoomed)
{
ZoomOutFromPlayer();
hasBeenZoomed = false;
}
}
}

void ZoomInToPlayer()
{
// By default the target x and y coordinates of the camera are it's current x and y coordinates.
float targetX = transform.position.x;
float targetY = transform.position.y;

// ... the target x coordinate should be a Lerp between the camera's current x position and the player's current x position.
targetX = Mathf.Lerp(transform.position.x, playerTransform.position.x, smooth * Time.deltaTime);
//Debug.Log("player x is " + playerTransform.position.x + " and TargetX is " + targetX);

// ... the target y coordinate should be a Lerp between the camera's current y position and the player's current y position.
targetY = Mathf.Lerp(transform.position.y, playerTransform.position.y, smooth * Time.deltaTime);
//Debug.Log("player y is " + playerTransform.position.y+ " and TargetY is " + targetY);


// Set the camera's position to the target position with the same z component.
cam.transform.position = new Vector3(targetX, targetY, transform.position.z);

// Change the size of the camera viewport
cam.orthographicSize = Mathf.Lerp(cam.orthographicSize, zoom, Time.deltaTime * smooth);
}

void ZoomOutFromPlayer()
{
// By default the target x and y coordinates of the camera are it's current x and y coordinates.
float targetX;
float targetY;

// Change the size of the camera viewport
cam.orthographicSize = Mathf.Lerp(cam.orthographicSize, normal, Time.deltaTime * smooth);

// ... the target x coordinate should be a Lerp between the camera's current x position and the original x position.
targetX = Mathf.Lerp(transform.position.x, LockedX, smooth * Time.deltaTime);
// ... the target y coordinate should be a Lerp between the camera's current y position and the original y position.
targetY = Mathf.Lerp(transform.position.y, LockedY, smooth * Time.deltaTime);
// Set the camera's position to the target position with the same z component.
cam.transform.position = new Vector3(targetX, targetY, transform.position.z);

}
}

最佳答案

您的方法 ZoomInToPlayerZoomOutFromPlayer 的编写方式建议在放大/缩小动画期间每帧调用一次。但是,ZoomOutFromPlayer 只会被调用一次,因为 Update,当 ZoomOutFromPlayer 被调用时,hasBeenZoomed 会立即被设置为假。

本质上,您在这里尝试做的是一个简单的 Finite State Machine .我建议多研究一下这种设计模式——它会帮助您注意到此类问题的根源,并以更好的方式构建您的代码。

在这种特殊情况下,在设计代码时防止此问题的一个好方法是在编写方法时为自己编写类似于“API 文档”的内容。对于 ZoomOutFromPlayer,它会读取如下内容:

Call every frame when you want to perform zoom-out animation, until the animation is complete.

在您编写(和阅读)这样的描述之后,您应该立即注意到一个危险信号 — “直到动画完成”?那么,调用这个方法的代码应该在一个单独的机制中以某种方式跟踪动画是否完成?真的很容易用错这个方法吗?好吧,这正是这里发生的事情。

相反,您可以做的是创建两种不同的方法,ZoomInUpdateZoomOutUpdate,其描述如下所示:

Call every frame when the camera should be zoomed out/zoomed in.

这样一来,使用这个方法就容易多了,你可以安全地用 hasBeenZoomed 抛出额外的逻辑。只需每帧调用这些方法,并确保(在这些方法内)它们以特定速度更改相机设置,如果这些设置需要更改,否则什么也不做。

关于c# - Unity 2D - 放大 = ok,缩小 = borked,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29791095/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com