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

时间:2012-11-15 13:46:18

标签: 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;
}
}

1 个答案:

答案 0 :(得分:1)

尝试将StartCoroutine function from MonoBehaviourWaitForSeconds instruction:

一起使用
...
StartCorountine(WaitForUnfreezeCamera());
...

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