为什么在使用断点时脚本会继续运行并且可以正常运行,但没有断点却无法运行?

时间:2018-10-23 03:06:00

标签: c# unity3d

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Playables;

public class SpaceshipCutscene : MonoBehaviour
{
    public Transform player;
    public Transform[] npcs;
    public Transform console;
    public Camera FPSCamera;
    public Camera mainCamera;
    public Animator[] anim;
    public float rotationSpeed = 3f;
    public float distanceFromConsole;


    private bool moveNpc = false;
    private float sp = 0f;
    private float distance;

    // Use this for initialization
    void Start()
    {

    }

    private void Update()
    {
        distance = Vector3.Distance(transform.position, npcs[0].transform.position);
        if (moveNpc)
        {
            // Soldier 2 rotating and looking at player
            Vector3 dir = player.position - npcs[0].position;
            dir.y = 0; // keep the direction strictly horizontal
            Quaternion rot = Quaternion.LookRotation(dir);
            // slerp to the desired rotation over time
            npcs[0].rotation = Quaternion.Slerp(npcs[0].rotation, rot, rotationSpeed * Time.deltaTime);

            var dist = Vector3.Distance(npcs[1].position, console.position);
            if (dist < distanceFromConsole)
            {
                sp += Time.deltaTime;
                sp = Mathf.Clamp(sp, 0f, 1f);
                anim[1].SetFloat("WalkingSpeed", sp);
            }

            Vector3 dirToComputer = console.transform.position - npcs[1].position;
            dirToComputer.y = 0;
            Quaternion rot1 = Quaternion.LookRotation(dirToComputer);
            npcs[1].rotation = Quaternion.Slerp(npcs[1].rotation, rot1, rotationSpeed * Time.deltaTime);

        }
    }

    private void OnTriggerExit(Collider other)
    {
        if (HoriDoorManager.doorLockState == false && distance < 5f)
        {
            if (other.gameObject.tag == "SpaceshipCutscene")
            {
                FPSCamera.enabled = false;
                mainCamera.enabled = true;
                moveNpc = true;
                anim[0].SetBool("Aiming", true);
                anim[1].SetBool("Walktouse", true);
            }
        }
    }
}

我正在计算播放器与第一个npc之间的距离,因为我希望OnTriggerExit触发并在特定方向上工作。

在行上使用断点时:

FPSCamera.enabled = false;

它在这一行上停止,然后我单击“继续”,它进入了Update中其余代码的内部。

但是,如果我没有在该行上添加断点,则该行将无法正常工作。不给出错误或异常只是不进入更新中的其余代码。

1 个答案:

答案 0 :(得分:3)

https://docs.unity3d.com/ScriptReference/Collider.OnTriggerExit.html 碰撞器停止接触后,在固定更新上发生OnTriggerExit

更新每帧发生一次,而FixedUpdate发生在计时器上。因此,这里的情况可能不同步。创建一个断点将确保框架停下来,并且在执行此操作时一切都可能对齐。