Home > Software design >  Getting a color from clicking the screen
Getting a color from clicking the screen

Time:08-07

I am currently making an earth in unity 3d, and I wanted to interact the earth by clicking it with mouse. To do this, I tried to use color.

Example image

For example, thinking that the blue thingy is my mouse pointer, if I click there, red will be returned.

However, since there was a distance between the camera and the earth, the system did not know where I was clicking on.

Does anyone know how to do this?

CodePudding user response:

Raycast from the mouse position and check if it hits any object and get it's color

RaycastHit2D hit = Physics2D.Raycast(Camera.main.ScreenToWorldPoint(Input.mousePosition), Vector2.zero);

if(hit.collider && hit.collider.TryGetComponent<Renderer>(out Renderer renderer)) {
    Color color = renderer.material.color;
}
  • Related