I am making a small evolution simulation, with creatures, food, water, and sickness. I need to somehow get the RGB values from raycasts sent out by the creatures so that they can tell what is food, water, or another creature, and can tell whether another creature is sick or not, which is determined by a color change. How would I use the raycast to get the color of the objects?
I don’t think you can from the raycasts alone. You could, potentially, get a component off a ray cast hit’s game object and gleam information from that (such as the material used by a mesh renderer).
Yet, with that in mind I wouldn’t even use colour to determine this (as it would be prone to so many other issues), but some values that you mutate on the components of said game objects.
Can you provide an example of how I would do that? I am not super advanced at coding, and there are many areas in which I have no experience (this being one of them)
Thanks! This worked!
You can get the texture coordinates (the UV) of a position hit by a raycast.
The official demo of Raycast has your original request in reverse. They’re writing black pixels to the original texture at the hit UV coordinates; you just want to read the original texture instead.
Hello !
For those who would need a little bit more context in their own game, to get the color of the objects that are hit by raycasts in Unity, you can do the following:
-
First, you need to create a script that will be attached to the game object that will be sending out the raycasts (e.g. the creature).
-
In the script, you need to define a function that will be responsible for sending out the raycasts and getting the color of the objects that are hit. This function should take a parameter that represents the distance that the raycast should travel.
-
To send out the raycast, you can use the RaycastHit function in Unity. This function takes in the origin of the raycast (usually the position of the game object), the direction of the raycast (usually the forward direction of the game object), and a RaycastHit object that will be used to store the information about the hit. You can also specify a layer mask to filter out specific layers that you don’t want to hit.
-
If the raycast hits something, you can access the color of the hit object by accessing the renderer component of the hit object and then the material property of the renderer. You can then access the color property of the material to get the color of the object.
Here’s an example of what the function could look like:
void GetColorFromRaycast(float distance)
{
RaycastHit hit;
if (Physics.Raycast(transform.position, transform.forward, out hit, distance))
{
Renderer renderer = hit.collider.gameObject.GetComponent<Renderer>();
if (renderer != null)
{
Color hitColor = renderer.material.color;
// You can now use the hitColor variable to access the color of the hit object.
}
}
}
You can then call this function whenever you want to get the color of an object that is hit by a raycast, and pass in the distance that you want the raycast to travel as an argument.
Hope this helps !
Wow, I’m also working on an evolution simulation that deals with colors I’m using an approach where the color is simply store as a value in a script. When the color value changes, it gets passed to the material, and other scripts can access the color too. Like you could raycast and check if you hit that component (an interface works well here) and request the color value. Much simpler and more reliable than getting the actual color of the pixel. You could do that with the camera, but the ray hitpoint might not even be visible to the camera and then of course you have lighting and material properties influencing it.
Be careful when using renderer.material. Consider using renderer.sharedMaterial instead. Using .material will instantiate the material and you will have to dispose of it manually when then object is destroyed.
I’d like to +1 what spiney said. Don’t bind your logic to your presentation. Also to read from textures you have to make them readable and fetch the hit UV position, which costs performance / memory.
Sorry for leaving you guys hanging, I had some stuff to do lol. I god the color component working using
rb = GetComponent<Rigidbody2D>();
eye1 = Physics2D.Raycast(transform.position, new Vector2(-1, 2), visionRange);
Color color1 = eye1.transform.gameObject.GetComponent<SpriteRenderer>().color;
and using if statements to check the color and preform actions based on their DNA. But now I’m wondering how I would make the line visible. Debug.Draw() won’t work because it doesn’t show up in the game view, so what would I do? Thanks!
LineRenderer is a component you can add to an object, and tell it where you want the points of a line or multiple connected lines. It’s a little bit cumbersome for drawing MANY lines, so there are some assets out there that can cover that gap if you need it.
make a sprite of a line and scale/rotate the transform based on your raycast. You can set the anchor of a sprite in the import settings, so for the line of sight you could place it at the bottom of the line which makes scaling and rotating a lot easier.