简体   繁体   English

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

[英]Freeze camera for 3 seconds in Unity3D (C#)

I'm trying to make the camera freeze for 3 seconds when the player scrolls to the left or right. 当玩家向左或向右滚动时,我试图让相机冻结3秒钟。 I've got the code working to scroll back as soon as you move the mouse back, but there must be a 3 second delay. 一旦你移回鼠标,我就有了代码可以向后滚动,但必须有3秒的延迟。 Here's that I've got: 这是我得到的:

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

Try use the StartCoroutine function from MonoBehaviour with a WaitForSeconds instruction: 尝试使用MonoBehaviourStartCoroutine函数WaitForSeconds指令:

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM