c# - 在 Unity3D (C#) 中卡住相机 3 秒

标签 c# time camera unity3d

当玩家向左或向右滚动时,我试图让相机卡住 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;
}
}

最佳答案

尝试使用 StartCoroutine function from MonoBehaviourWaitForSeconds instruction:

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

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

关于c# - 在 Unity3D (C#) 中卡住相机 3 秒,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13398777/

相关文章:

linux - 在 Linux 上的 gphoto2 中连拍拍摄照片

Android - 使用相机 Intent (ACTION_VIDEO_CAPTURE) 显示不同的方向

c# - 将相机从当前位置平稳地移动到特定位置?

c# - 当计时器线程没有按时完成时会发生什么

c# - Visual Studio 2012 Express for Web 不运行单元测试

c# - 如何在 MongoDB 中为不同的字段名称注册 ScalarDiscriminatorConvention?

html - 计时器 + Aframe : How to use setTimeOut for Aframe. 寄存器组件 ("...")

ruby-on-rails - 如何在两个日期时间字段的 rails 中获得以分钟为单位的时差?

iphone - NSDateFormatter dateFromString 和 iPhone 中的 24 小时格式混淆

c# - 跳过映射空属性