예제 #1
0
    void Update()
    {
        detectDoor = GameObject.FindWithTag("MainCamera").GetComponent <DetectObject>(); // This will give me the state of DetectDoor from the DetectObject script
        if (detectDoor.DetectDoor == true)

        {
            if (Locked == false)                           // If the key was destroyed the door will open and play the Door_Open audio clip
            {
                StateMachine.SetBool(AnimationName, true); // Starts the DoorOpen animation by setting the DoorOpenBool to true
                soundSource.clip = soundFiles[0];
                soundSource.Play();
            }
            else
            {
                soundSource.clip = soundFiles[1]; // If the key has not been found and the game object is not destroyed, the door will not open and the Door_Locked audio clip will play
                soundSource.Play();
            }
        }
    }
예제 #2
0
    // Update is called once per frame
    void Update()
    {
        if (gd_loop)
        {
            urg.Write(SCIP_library.SCIP_Writer.GD(0, 1080));
        }

        // center offset rect
        Rect detectAreaRect = areaRect;

        detectAreaRect.x      *= scale;
        detectAreaRect.y      *= scale;
        detectAreaRect.width  *= scale;
        detectAreaRect.height *= scale;

        detectAreaRect.x = -detectAreaRect.width / 2;
        //

        float d      = Mathf.PI * 2 / 1440;
        float offset = d * 540;

        // cache directions
        if (urg.distances.Count > 0)
        {
            if (!cached)
            {
                directions = new Vector3[urg.distances.Count];
                for (int i = 0; i < directions.Length; i++)
                {
                    float a = d * i + offset;
                    directions[i] = new Vector3(-Mathf.Cos(a), -Mathf.Sin(a), 0);
                }
                cached = true;
            }
        }

        // strengths
        try{
            if (urg.strengths.Count > 0)
            {
                strengths.Clear();
                strengths.AddRange(urg.strengths);
            }
        }catch {
        }
        // distances
        try{
            if (urg.distances.Count > 0)
            {
                distances.Clear();
                distances.AddRange(urg.distances);
            }
        }catch {
        }
//		List<long> distances = urg.distances;

        if (debugDraw)
        {
            // strengths
            for (int i = 0; i < strengths.Count; i++)
            {
                //float a = d * i + offset;
                //Vector3 dir = new Vector3(-Mathf.Cos(a), -Mathf.Sin(a), 0);
                Vector3 dir  = directions[i];
                long    dist = strengths[i];
                Debug.DrawRay(Vector3.zero, Mathf.Abs(dist) * dir * scale, strengthColor);
            }

            // distances
//			float colorD = 1.0f / 1440;
            for (int i = 0; i < distances.Count; i++)
            {
                //float a = d * i + offset;
                //Vector3 dir = new Vector3(-Mathf.Cos(a), -Mathf.Sin(a), 0);
                Vector3 dir  = directions[i];
                long    dist = distances[i];
                //color = (dist < limit && dir.y > 0) ? detectColor : new Color(colorD * i, 0,0,1.0f);
                //Color color = (dist < limit && dir.y > 0) ? detectColor : distanceColor;
//				Debug.DrawRay(Vector3.zero, dist * dir * scale, color);
                Debug.DrawRay(Vector3.zero, dist * dir * scale, distanceColor);
            }
        }

        //-----------------
        //  group
        detectObjects = new List <DetectObject>();
        //
        //------
//		bool endGroup = true;
//		for(int i = 0; i < distances.Count; i++){
//			int id = i;
//			long dist = distances[id];
//
//			float a = d * i + offset;
//			Vector3 dir = new Vector3(-Mathf.Cos(a), -Mathf.Sin(a), 0);
//
//			if(dist < limit && dir.y > 0){
//				DetectObject detect;
//				if(endGroup){
//					detect = new DetectObject();
//					detect.idList.Add(id);
//					detect.distList.Add(dist);
//
//					detect.startDist = dist;
//					detectObjects.Add(detect);
//
//					endGroup = false;
//				}else{
//					detect = detectObjects[detectObjects.Count-1];
//					detect.idList.Add(id);
//					detect.distList.Add(dist);
//
//					if(dist > detect.startDist){
//						endGroup = true;
//					}
//				}
//			}else{
//				endGroup = true;
//			}
//		}

        //------
//		bool endGroup = true;
//		for(int i = 1; i < distances.Count-1; i++){
//			long dist = distances[i];
//			float delta = Mathf.Abs((float)(distances[i] - distances[i-1]));
//			float delta1 = Mathf.Abs((float)(distances[i+1] - distances[i]));
//
//			float a = d * i + offset;
//			Vector3 dir = new Vector3(-Mathf.Cos(a), -Mathf.Sin(a), 0);
//
//			if(dir.y > 0){
//				DetectObject detect;
//				if(endGroup){
//					if(dist < limit && delta > 50){
//						detect = new DetectObject();
//						detect.idList.Add(i);
//						detect.distList.Add(dist);
//
//						detect.startDist = dist;
//						detectObjects.Add(detect);
//
//						endGroup = false;
//					}
//				}else{
//					if(delta < 50){
//						detect = detectObjects[detectObjects.Count-1];
//						detect.idList.Add(i);
//						detect.distList.Add(dist);
//					}else{
//						endGroup = true;
//					}
//				}
//			}
//		}


        //------
        bool  endGroup   = true;
        float deltaLimit = 100;         // 認識の閾値 連続したもののみを取得するため (mm)

        for (int i = 1; i < distances.Count - 1; i++)
        {
            //float a = d * i + offset;
            //Vector3 dir = new Vector3(-Mathf.Cos(a), -Mathf.Sin(a), 0);
            Vector3 dir    = directions[i];
            long    dist   = distances[i];
            float   delta  = Mathf.Abs((float)(distances[i] - distances[i - 1]));
            float   delta1 = Mathf.Abs((float)(distances[i + 1] - distances[i]));

            if (dir.y > 0)
            {
                DetectObject detect;
                if (endGroup)
                {
                    Vector3 pt = dist * dir * scale;
                    if (dist < limit && (delta < deltaLimit && delta1 < deltaLimit))
                    {
//					bool isArea = detectAreaRect.Contains(pt);
//					if(isArea && (delta < deltaLimit && delta1 < deltaLimit)){
                        detect = new DetectObject();
                        detect.idList.Add(i);
                        detect.distList.Add(dist);

                        detect.startDist = dist;
                        detectObjects.Add(detect);

                        endGroup = false;
                    }
                }
                else
                {
                    if (delta1 >= deltaLimit || delta >= deltaLimit)
                    {
                        endGroup = true;
                    }
                    else
                    {
                        detect = detectObjects[detectObjects.Count - 1];
                        detect.idList.Add(i);
                        detect.distList.Add(dist);
                    }
                }
            }
        }

        //-----------------
        // draw
        drawCount = 0;
        for (int i = 0; i < detectObjects.Count; i++)
        {
            DetectObject detect = detectObjects[i];

            // noise
            if (detect.idList.Count < noiseLimit)
            {
                continue;
            }

            int offsetCount = detect.idList.Count / 3;
            int avgId       = 0;
            for (int n = 0; n < detect.idList.Count; n++)
            {
                avgId += detect.idList[n];
            }
            avgId = avgId / (detect.idList.Count);

            long avgDist = 0;
            for (int n = offsetCount; n < detect.distList.Count - offsetCount; n++)
            {
                avgDist += detect.distList[n];
            }
            avgDist = avgDist / (detect.distList.Count - offsetCount * 2);

            //float a = d * avgId + offset;
            //Vector3 dir = new Vector3(-Mathf.Cos(a), -Mathf.Sin(a), 0);
            Vector3 dir  = directions[avgId];
            long    dist = avgDist;


            //float a0 = d * detect.idList[offsetCount] + offset;
            //Vector3 dir0 = new Vector3(-Mathf.Cos(a0), -Mathf.Sin(a0), 0);
            int     id0   = detect.idList[offsetCount];
            Vector3 dir0  = directions[id0];
            long    dist0 = detect.distList[offsetCount];

            //float a1 = d * detect.idList[detect.idList.Count-1 - offsetCount] + offset;
            //Vector3 dir1 = new Vector3(-Mathf.Cos(a1), -Mathf.Sin(a1), 0);
            int     id1   = detect.idList[detect.idList.Count - 1 - offsetCount];
            Vector3 dir1  = directions[id1];
            long    dist1 = detect.distList[detect.distList.Count - 1 - offsetCount];

            Color gColor;
            if (drawCount < groupColors.Length)
            {
                gColor = groupColors[drawCount];
            }
            else
            {
                gColor = Color.green;
            }
            for (int j = offsetCount; j < detect.idList.Count - offsetCount; j++)
            {
                //float _a = d * detect.idList[j] + offset;
                //Vector3 _dir = new Vector3(-Mathf.Cos(_a), -Mathf.Sin(_a), 0);
                int     _id   = detect.idList[j];
                Vector3 _dir  = directions[_id];
                long    _dist = detect.distList[j];
                Debug.DrawRay(Vector3.zero, _dist * _dir * scale, gColor);
            }

            Debug.DrawLine(dist0 * dir0 * scale, dist1 * dir1 * scale, gColor);
            Debug.DrawRay(Vector3.zero, dist * dir * scale, Color.green);

            drawCount++;
        }

        DrawRect(detectAreaRect, Color.green);
    }
예제 #3
0
 void Update()
 {
     detectKey = GameObject.FindWithTag("MainCamera").GetComponent <DetectObject>(); // This will give me the state of DetectKey from the DetectObject script
 }
예제 #4
0
        private void CalcUrgPosition()
        {
            m_DebugLineVector3.Clear();
            m_UrgGUI.Clear();

            if (m_UrgDevice == null)
            {
                return;
            }

            // cache directions
            if (m_UrgDevice.distances.Count > 0)
            {
                if (!m_Cached)
                {
                    float d      = Mathf.PI * 2 / 1440;
                    float offset = d * 540;

                    m_Directions = new Vector3[m_UrgDevice.distances.Count];
                    for (int i = 0; i < m_Directions.Length; i++)
                    {
                        float a = d * i + offset;
                        //directions[i] = new Vector3(-Mathf.Cos(a), -Mathf.Sin(a), 0);
                        m_Directions[i] = new Vector3(Mathf.Cos(a), Mathf.Sin(a), 0);
                    }
                    m_Cached = true;
                }
            }

            // strengths
            try
            {
                if (m_UrgDevice.strengths.Count > 0)
                {
                    m_Strengths.Clear();
                    m_Strengths.AddRange(m_UrgDevice.strengths);
                }
            }
            catch
            {
            }
            // distances
            try
            {
                if (m_UrgDevice.distances.Count > 0)
                {
                    m_Distances.Clear();
                    m_Distances.AddRange(m_UrgDevice.distances);
                }
            }
            catch
            {
            }
            if (m_DebugDraw)
            {
                //// strengths
                //for (int i = 0; i < m_Strengths.Count; i++)
                //{
                //    Vector3 dir = m_Directions[i];
                //    long dist = m_Strengths[i];
                //    //Debug.DrawRay(Vector3.zero, Mathf.Abs(dist) * dir * m_Scale, m_StrengthColor);
                //}

                for (int i = 0; i < m_Distances.Count; i++)
                {
                    Vector3 dir  = m_Directions[i];
                    long    dist = m_Distances[i];

                    Vector3 rayend = dist * dir * m_Scale;
                    //Debug.DrawRay(Vector3.zero, rayend, m_DistanceColor);


                    m_UrgGUI.Add(new UrgGUIData()
                    {
                        start_pos = new Vector3(m_ResolutionWidth / 2, m_ResolutionHeight),
                        end_pos   = rayend + new Vector3(m_ResolutionWidth / 2, m_ResolutionHeight) + OriginalOffset,
                        color     = m_DistanceColor,
                    });
                }
            }

            //  group
            m_DetectObjects.Clear();
            //------
            bool  endGroup   = true;
            float deltaLimit = 100; // 识别阈值仅获取连续值(mm)

            for (int i = 1; i < m_Distances.Count - 1; i++)
            {
                Vector3 dir    = m_Directions[i];
                long    dist   = m_Distances[i];
                float   delta  = Mathf.Abs((float)(m_Distances[i] - m_Distances[i - 1]));
                float   delta1 = Mathf.Abs((float)(m_Distances[i + 1] - m_Distances[i]));

                if (dir.y < 0)
                {
                    DetectObject detect;
                    if (endGroup)
                    {
                        //Vector3 pt = dist * dir * m_Scale;
                        if (dist < m_Limit && (delta < deltaLimit && delta1 < deltaLimit))
                        {
                            //bool isArea = detectAreaRect.Contains(pt);
                            //if(isArea && (delta < deltaLimit && delta1 < deltaLimit)){
                            detect = new DetectObject();
                            detect.idList.Add(i);
                            detect.distList.Add(dist);

                            detect.startDist = dist;
                            m_DetectObjects.Add(detect);

                            endGroup = false;
                        }
                    }
                    else
                    {
                        if (delta1 >= deltaLimit || delta >= deltaLimit)
                        {
                            endGroup = true;
                        }
                        else
                        {
                            detect = m_DetectObjects[m_DetectObjects.Count - 1];
                            detect.idList.Add(i);
                            detect.distList.Add(dist);
                        }
                    }
                }
            }

            //-----------------
            // draw
            m_DrawCount = 0;
            for (int i = 0; i < m_DetectObjects.Count; i++)
            {
                DetectObject detect = m_DetectObjects[i];

                // noise
                if (detect.idList.Count < m_NoiseLimit)
                {
                    continue;
                }

                int offsetCount = detect.idList.Count / 3;
                int avgId       = 0;
                for (int n = 0; n < detect.idList.Count; n++)
                {
                    avgId += detect.idList[n];
                }
                avgId = avgId / (detect.idList.Count);

                long avgDist = 0;
                for (int n = offsetCount; n < detect.distList.Count - offsetCount; n++)
                {
                    avgDist += detect.distList[n];
                }
                avgDist = avgDist / (detect.distList.Count - offsetCount * 2);

                Vector3 dir  = m_Directions[avgId];
                long    dist = avgDist;

                if (m_DebugDraw)
                {
                    int     id0   = detect.idList[offsetCount];
                    Vector3 dir0  = m_Directions[id0];
                    long    dist0 = detect.distList[offsetCount];

                    int     id1   = detect.idList[detect.idList.Count - 1 - offsetCount];
                    Vector3 dir1  = m_Directions[id1];
                    long    dist1 = detect.distList[detect.distList.Count - 1 - offsetCount];

                    Color gColor;
                    if (m_DrawCount < m_GroupColors.Length)
                    {
                        gColor = m_GroupColors[m_DrawCount];
                    }
                    else
                    {
                        gColor = Color.green;
                    }
                    for (int j = offsetCount; j < detect.idList.Count - offsetCount; j++)
                    {
                        int     _id   = detect.idList[j];
                        Vector3 _dir  = m_Directions[_id];
                        long    _dist = detect.distList[j];

                        Vector3 rayend = _dist * _dir * m_Scale;
                        //Debug.DrawRay(Vector3.zero, rayend, gColor);

                        m_UrgGUI.Add(new UrgGUIData()
                        {
                            start_pos = new Vector3(m_ResolutionWidth / 2, m_ResolutionHeight),
                            end_pos   = rayend + new Vector3(m_ResolutionWidth / 2, m_ResolutionHeight) + OriginalOffset,
                            color     = gColor,
                        });
                    }
                    Vector3 start = dist0 * dir0 * m_Scale;
                    Vector3 end   = dist1 * dir1 * m_Scale;
                    //Debug.DrawLine(start, end, gColor);
                    m_UrgGUI.Add(new UrgGUIData()
                    {
                        start_pos = start + new Vector3(m_ResolutionWidth / 2, m_ResolutionHeight) + OriginalOffset,
                        end_pos   = end + new Vector3(m_ResolutionWidth / 2, m_ResolutionHeight) + OriginalOffset,
                        color     = gColor,
                    });
                }

                Vector3 point = dist * dir * m_Scale;
                // 筛选的中间点
                //Debug.DrawRay(Vector3.zero, point, Color.red);

                Vector3 ScreenPoint = point + new Vector3(m_ResolutionWidth / 2, m_ResolutionHeight) + OriginalOffset;
                m_DebugLineVector3.Add(ScreenPoint);

                GameEntry.Windows.WallUICameraRay(ScreenPoint);

                m_DrawCount++;
            }
        }
예제 #5
0
 void Update()
 {
     detectSign = GameObject.FindWithTag("MainCamera").GetComponent <DetectObject>();
 }