- Home /
 
 
               Question by 
               sassykorn · Mar 15, 2021 at 11:50 PM · 
                colorcolorpicker  
              
 
              Help,How to fix this?

so my 3rd line goes off into the stratosphere and it's supposed to be on the right centre of the same distance, tried a buncha stuff, can't figure it out, pls help.
Code:
public class ColorPick : MonoBehaviour { public GameObject colorIndicatorInstance; public Color pickedColor = Color.red; Color pickedOne = Color.white; Color pickedTwo = Color.white; Color pickedThree = Color.white; Color pickedFour = Color.white; Texture2D texture; Texture2D texture2; Texture2D texture3; Texture2D texture4;
 bool mouseActive = false;
 private Vector2 mousePos;
 private Vector2 mousePosTwo;
   
 private Vector2 mousePosComp;
 private Vector2 mousePosCompTwo;
   
 private Camera camera;
 private GameObject colorIndicator1;
 private GameObject colorIndicator2;
 private GameObject colorIndicator3;
 private GameObject colorIndicator4;
 // Start is called before the first frame update
 void Start()
 {
     colorIndicator1 = Instantiate(colorIndicatorInstance, new Vector3(0, 0, -100), Quaternion.Euler(new Vector3(90, 0, 0)));
     colorIndicator2 = Instantiate(colorIndicatorInstance, new Vector3(0, 0, -100), Quaternion.Euler(new Vector3(90, 0, 0)));
     colorIndicator3 = Instantiate(colorIndicatorInstance, new Vector3(0, 0, -100), Quaternion.Euler(new Vector3(90, 0, 0)));
     colorIndicator4 = Instantiate(colorIndicatorInstance, new Vector3(0, 0, -100), Quaternion.Euler(new Vector3(90, 0, 0)));
     camera = this.GetComponent<Camera>();
     mousePos = mousePosComp = new Vector2(camera.pixelWidth / 2.0f, camera.pixelHeight / 2.0f);
     mousePosTwo = mousePosCompTwo = new Vector2(camera.pixelWidth / 2.0f, camera.pixelHeight / 2.0f);
     
     texture = new Texture2D(1, 1);
     texture.SetPixel(0, 0, pickedColor);
     texture.Apply();
     texture2 = new Texture2D(1, 1);
     texture2.SetPixel(0, 0, pickedColor);
     texture2.Apply();
     texture3 = new Texture2D(1, 1);
     texture3.SetPixel(0, 0, pickedColor);
     texture3.Apply();
     texture4 = new Texture2D(1, 1);
     texture4.SetPixel(0, 0, pickedColor);
     texture4.Apply();
 }
 private Color GetColorAtMousePos(Vector2 mousePos)
 {
     RaycastHit hit;
     Ray ray = camera.ScreenPointToRay(mousePos);
     Physics.Raycast(ray, out hit);
     Color c = Color.white;
     if (hit.collider)
     {
         Texture2D tex = (Texture2D)hit.collider.GetComponent<Renderer>().material.mainTexture; // Get texture of object under mouse pointer
         c = tex.GetPixelBilinear(hit.textureCoord2.x, hit.textureCoord2.y); // Get color from texture
         
     }
     return c;
 }
   
 void DrawLines(int mode)
 {
     if (mouseActive)
     {
         mousePos = Input.mousePosition;
         mousePosComp = new Vector2(camera.pixelWidth - mousePos.x, camera.pixelHeight - mousePos.y);
         mousePosTwo = Input.mousePosition;
         mousePosCompTwo = new Vector2(camera.pixelWidth + mousePosTwo.x, camera.pixelHeight - mousePos.y);
         pickedOne = GetColorAtMousePos(mousePos);
         pickedTwo = GetColorAtMousePos(mousePosComp);
         pickedThree = GetColorAtMousePos(mousePosCompTwo);
         pickedFour = GetColorAtMousePos(mousePosCompTwo);
     }
     Vector3 mousePos3 = camera.ScreenToWorldPoint(mousePos);
     Vector3 mousePos3Comp = camera.ScreenToWorldPoint(mousePosComp);
     Vector3 mousePos3Two = camera.ScreenToWorldPoint(mousePosTwo);
     Vector3 mousePos3TwoComp = camera.ScreenToWorldPoint(mousePosCompTwo);
     switch (mode)
     {
         
         default:
             //1
             Debug.DrawLine(new Vector3(0, 0, 0), mousePos3, Color.black);
             colorIndicator1.transform.position = mousePos3;
             colorIndicator1.GetComponent<MeshRenderer>().material.color = pickedOne;
             colorIndicator1.transform.GetChild(0).GetComponent<MeshRenderer>().material.color = Color.black;
             Debug.DrawLine(new Vector3(0, 0, 0), mousePos3Comp, Color.black);
             colorIndicator2.transform.position = mousePos3Comp;
             colorIndicator2.GetComponent<MeshRenderer>().material.color = pickedTwo;
             colorIndicator2.transform.GetChild(0).GetComponent<MeshRenderer>().material.color = Color.black;
             //2
             //Debug.DrawLine(new Vector3(0, 0, 0), mousePos3Two, Color.black);
             //colorIndicator3.transform.position = mousePos3Two;
             //colorIndicator3.GetComponent<MeshRenderer>().material.color = pickedThree;
             //colorIndicator3.transform.GetChild(0).GetComponent<MeshRenderer>().material.color = Color.black;
             Debug.DrawLine(new Vector3(0, 0, 0), mousePos3TwoComp, Color.black);
             colorIndicator3.transform.position = mousePos3TwoComp;
             colorIndicator3.GetComponent<MeshRenderer>().material.color = pickedThree;
             colorIndicator3.transform.GetChild(0).GetComponent<MeshRenderer>().material.color = Color.black;
             break;
         case 2:
             //2
             //Debug.DrawLine(new Vector3(0, 0, 0), mousePos3Two, Color.black);
             //colorIndicator3.transform.position = mousePos3Two;
             //colorIndicator3.GetComponent<MeshRenderer>().material.color = pickedThree;
             //colorIndicator3.transform.GetChild(0).GetComponent<MeshRenderer>().material.color = Color.black;
             //Debug.DrawLine(new Vector3(0, 0, 0), mousePos3TwoComp, Color.black);
             //colorIndicator3.transform.position = mousePos3TwoComp;
             //colorIndicator3.GetComponent<MeshRenderer>().material.color = pickedThree;
             //colorIndicator3.transform.GetChild(0).GetComponent<MeshRenderer>().material.color = Color.black;
             break;
     }
     
 }
 // Update is called once per frame
 void Update()
 {
     DrawLines(0);
     texture.SetPixel(0, 0, pickedOne);
     texture.Apply();
     texture2.SetPixel(0, 0, pickedTwo);
     texture2.Apply();
     texture3.SetPixel(0, 0, pickedThree);
     texture3.Apply();
     texture4.SetPixel(0, 0, pickedFour);
     texture4.Apply();
 }
 private void OnGUI()
 {
     if (Event.current.type == EventType.MouseDown)
     {
         mouseActive = !mouseActive;
     }
     GUI.skin.box.normal.background = texture;
     GUI.Box(new Rect(25,25, 75, 75),GUIContent.none);
     GUI.skin.box.normal.background = texture2;
     GUI.Box(new Rect(25,150, 75, 75),GUIContent.none);
     GUI.skin.box.normal.background = texture3;
     GUI.Box(new Rect(25,275, 75, 75), GUIContent.none);
     GUI.skin.box.normal.background = texture4;
     GUI.Box(new Rect(25, 400, 75, 75), GUIContent.none);
 }
 
               }
 
                 
                this-bitch.png 
                (189.5 kB) 
               
 
              
               Comment
              
 
               
              Your answer