在Unity3D(C#)中冻结相机3秒。

4

我正在尝试使相机在玩家向左或向右滚动时冻结3秒钟。 我已经让代码在你把鼠标移回来的时候滚动回去,但需要有3秒的延迟。 以下是我的代码:

using UnityEngine;
using System.Collections;

public class CamCont : MonoBehaviour {
public float LockedY = 1;
public float LockedZ = -7;
public GameObject player;

private bool edgeRightMouse = false;
private bool edgeLeftMouse = false;
private float backLeft = -0.3f;
private float backRight = 0.3f;
private bool backLStrife = false;
private bool backRStrife = false;
private float freezeOn = 0.0f;
private float freezeUntil = 3.0f;
private float plusSpeed = 0.1f;
private float minusSpeed = -0.1f;

public float sensitivityX = 1f;
public float horizontalMouseRight = 1014;
public float horizontalMouseLeft = 10;
public float moveRightUntil = 20;
public float moveLeftUntil = -20;

float mHdg = 0f;
float mPitch = 0f;

void Start() {
    //:P
}

void Update() {
    if (Input.mousePosition.x > horizontalMouseRight) {
        if (transform.position.x < moveRightUntil) {
            Strafe (plusSpeed);
            edgeRightMouse = true;
        }
    }
    else edgeRightMouse = false;

    if (transform.position.x > player.transform.position.x && !edgeRightMouse) {
        /*if (backLStrife == false) {
            freezeOn >= Time.deltaTime;
            if (freezeOn >= freezeUntil) {
                backLStrife = true;
            }
        }*/
        if (backLStrife == false)    backLStrife = true;
        if (backLStrife == true)    Strafe (backLeft);
        if (transform.position.x - player.transform.position.x < backRight)
            backLStrife = false;
    }

    if (Input.mousePosition.x < horizontalMouseLeft) {
        if (transform.position.x > moveLeftUntil) {
            Strafe (minusSpeed);
            edgeLeftMouse = true;
        }
    }
    else edgeLeftMouse = false;

    if (transform.position.x < player.transform.position.x && !edgeLeftMouse) {
        if (backRStrife == false && freezeOn >= freezeUntil)    backRStrife = true;
        if (backRStrife == false)    backRStrife = true;
        if (backRStrife == true)    Strafe (backRight);
        if (transform.position.x + player.transform.position.x > backLeft)
            backRStrife = false;
    }

    if (!backLStrife && !backRStrife && !edgeLeftMouse && !edgeRightMouse && freezeOn > 0)
        transform.position = new Vector3(player.transform.position.x, LockedY, LockedZ);
    Debug.Log (Input.mousePosition);
}

void Strafe(float aVal) {
    transform.position += aVal * transform.right;
}

void ChangeHeading(float aVal) {
    mHdg += aVal;
    WrapAngle(ref mHdg);
    transform.localEulerAngles = new Vector2(mPitch, mHdg);
}

public static void WrapAngle(ref float angle) {
    if (angle < -360f)
    angle += 360f;
    if (angle > 360f)
    angle -= 360f;
}
}

也许这只是一个临时的解决方案,而且不确定它是否有效。但如果将滚动方法放在不同的线程上,并且当代码调用它时让线程休眠3000毫秒,会发生什么呢? - CuccoChaser
@Floris 这听起来太肮脏了,即使是为了快速解决问题也不行...!! - Darius
@florisprijt Unity不喜欢破坏其线程模型。 - Jerdak
1个回答

1

尝试使用 MonoBehaviour 中的 StartCoroutine 函数WaitForSeconds 指令

...
StartCorountine(WaitForUnfreezeCamera());
...

IEnumerator WaitForUnfreezeCamera()
{
    yield return new WaitForSeconds(3f);
    // your code to unfreeze the camera.
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接