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;
}
}
4

1 回答 1

1

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

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

IEnumerator WaitForUnfreezeCamera()
{
    yield return new WaitForSeconds(3f);
    // your code to unfreeze the camera.
}
于 2012-11-15T14:27:24.457 回答