Hello I am trying to make an ar game but the response is lagging here's the code Code (CSharp): public class Controls : MonoBehaviour { public float speed; private WebCamTexture cam; public GameObject picture; private int[] points = new int [9];//The points with 0=UL and 9=DR int a, b, medianX, medianY; private Rigidbody rb; private Vector3 move = new Vector3(0,0,0); // Use this for initialization void Start () { rb = GetComponent<Rigidbody>(); cam = new WebCamTexture(); picture.GetComponent<Renderer>().material.mainTexture = cam; cam.Play(); medianX = cam.width / 2; medianY = cam.height / 2; a = (cam.width*20) / 100; b = (cam.height*20) / 100; } // Update is called once per frame void Update () { for (int i=0;i<cam.height;i++) { for (int j=0;j<cam.width;j++) { float h, s, v; Color.RGBToHSV(cam.GetPixel(j,i),out h,out s,out v); if (h*360>=210 && h*360<=220 && s*100>=70 && v*100>=30) { if (j >= medianX - a && j <= medianX + a) { if (i>=0 && i<=medianY-b-1) { points[7]++; } else if (i>=medianY-b && i<=medianY+b) { points[4]++; }else { points[1]++; } } else if (j>=0 && j<=medianX-a-1) { if (i>=0 && i<=medianY-b-1) { points[6]++; } else if (i>=medianY-b && i<=medianY+b) { points[3]++; } else { points[0]++; } }else { if (i>=0 && i<=medianY-b-1) { points[8]++; } else if (i>=medianY-b && i<=medianY+b) { points[5]++; } else { points[2]++; } } } } } int max = 0; for (int i = 1; i < points.Length; i++){ if (points[max]<=points[i]) { max = i; } } Vector3 move = new Vector3(0,0,0); if (max==0) { move = new Vector3(-1,0,1); } else if (max==1) { move = new Vector3(0,0,1); } else if (max==2) { move = new Vector3(1,0,1); } else if (max==3) { move = new Vector3(-1,0,0); } else if (max==4) { move = new Vector3(0,0,0); } else if (max==5) { move = new Vector3(1,0,0); } else if (max==6) { new Vector3(-1,0,-1); } else if (max==7) { new Vector3(0,0,-1); } else{ new Vector3(1, 0, -1); } Debug.Log(max); rb.velocity = move * speed; if (Input.GetKeyUp(KeyCode.Space)) { ptrSc(cam); } } private void ptrSc(WebCamTexture cam) { StreamWriter sw = new StreamWriter("C:\\Users\\George\\Desktop\\Projects\\Game3\\Pic.txt", false); for (int i=cam.height-1;i>=0;i--) { for (int j=cam.width-1;j>=0; j--) { float h, s, v; Color.RGBToHSV(cam.GetPixel(j,i),out h,out s,out v); if (h * 360 >= 210 && h * 360 <= 220 && s * 100 >= 70 && v * 100 >= 30) { sw.Write(255 + " "); }else { sw.Write(0 + " "); } } sw.WriteLine(); } } } The code is recognize the blue cap of a pencil cap and its splits the array at 9 pieces checks every pixel for the color(condition) and if it finds one pixel sum the region. UL=0 UC=1 UR=2 CL=3 CC=4 CR=5 DL=6 DR=8 DC=7