- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我希望有人能提供帮助。我正在尝试创建一个小脚本来放大我的播放器并退出 - 切换。放大效果很好,但是当我尝试缩小时它不起作用,它卡住了。我创建了一个 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);
}
}
最佳答案
您的方法 ZoomInToPlayer
和 ZoomOutFromPlayer
的编写方式建议在放大/缩小动画期间每帧调用一次。但是,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.
在您编写(和阅读)这样的描述之后,您应该立即注意到一个危险信号 — “直到动画完成”?那么,调用这个方法的代码应该在一个单独的机制中以某种方式跟踪动画是否完成?真的很容易用错这个方法吗?好吧,这正是这里发生的事情。
相反,您可以做的是创建两种不同的方法,ZoomInUpdate
和 ZoomOutUpdate
,其描述如下所示:
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/
我正在使用以下代码读取我在文本编辑器 (Notepad++) 中创建的文本 (.xml) 文件,将我从中读取的 UTF-8 文本转换为 UTF-16,以便 Windows API 函数可以使用它,然后
关闭。这个问题是opinion-based .它目前不接受答案。 想改进这个问题?更新问题,以便 editing this post 可以用事实和引用来回答它. 1年前关闭。 Improve this
我知道所有的论坛都充满了这样的问题,但我尝试了几个钩子(Hook),但它们不起作用(或者我做的不好)。 所以,我有: main.cpp <- fawn.h <- connector.cpp (defe
这是我正在使用的一段代码。 当 items 为 null 时,ok(Object items) 方法在内部调用 Jersey 的 Response.ok() 方法。 MembershipReq
我在 Tcl/Tk 中有一个简单的窗口,点击确定按钮运行模拟。我正在使用 Linux。模拟完成后,窗口将被销毁。问题是在模拟运行时窗口仍然存在。我希望窗口在我点击确定按钮后消失。 我尝试使用 wm w
在我们的网络应用程序中,我将 Angular-Materials $mdDialog 与确认对象一起使用。是否可以将按钮的顺序从取消-确定更改为确定-取消?并将初始焦点设置为取消按钮?也许通过 CSS
这个问题在这里已经有了答案: 关闭10年前。 Possible Duplicate: Ternary conditional operator in Python 我有这个问题,不知道要问谷歌: (v
我尝试使用 R 进行回归。我有以下代码,导入 CSV 文件没有问题 dat <- read.csv('http://pastebin.com/raw.php?i=EWsLjKNN',sep="
在 QInputDialog 中,如何去掉 OK 和 Cancel 按钮中的图标? 注意取消和确定的图标。我查看了属性按钮,不知道如何删除它们。 最佳答案 解决方案的策略是先获取按钮,但是这些属于QD
当使用Postman测试项目时,任何POST方法,我收到的是200 OK而不是201 Created,并且subreddit不是在数据库中创建的,并且在控制台休眠中接收到以下内容:SELECT T1_
当使用Postman测试项目时,任何POST方法,我收到的是200 OK而不是201 Created,并且subreddit不是在数据库中创建的,并且在控制台休眠中接收到以下内容:SELECT T1_
我制作了一个安卓应用程序,可以从本地 wifi 网络传输语音。为了收听和流式传输,我使用 JNI 中的 Opus C API 进行解码,并使用 OpenSL Audio 进行读取。 我从 Servic
我有一个定义如下的 map : mapMeasures := make(map[time.Time]models.Measure, 0) 与 type Measure struct { Del
这里我的数据集是 pd我已将其拆分为训练和测试数据 pd_train1和 pd_train2 sku national_inv lead_time in_transit_qty forecas
我已经检查过有关此问题的其他问题,但由于问题似乎非常具体,因此它们没有帮助。 我有一个像这样的数据框(这只是一个简单的示例,下面提供了来自 dput() 的示例数据): year species ab
当我使用 anova_test() 函数(来自 rstatix 包)做双向重复测量方差分析时,出现错误: lm.fit(x, y, offset = offset, singular.ok = sin
我一直在尝试对数据集进行 2-Way 重复测量测试,年份和疫苗类型是自变量,覆盖率是因变量。我用代码运行它: sat = anova_test( data=SA, dv = coverage, w
奇遇 我是一个普通的大学生,尹成是我的名字。顾名思义,我和其他人一样,没有什么特别之处。然而,在某个偶然的机会下,我发现了一个全新的领域——编程。 初印象 说实话,我对编程并不了解,甚至可以说是一窍不
我使用这些代码创建了一个卡拉 OK 并将其刻录到 VCD。 xxxxxx_1.m4a 文件是左声道(乐器),xxxxxx_0.m4a 文件是右声道(带人声的歌曲)。 将它们组合到 become xxx
我不确定为什么会发生这种情况,但我有一个简单的 Ajax 代码: $.ajax({ url: "/javascript/testing.js"}) .done(function(data){
我是一名优秀的程序员,十分优秀!