- Home /
Get terrain texture
I'm working on a game with a golf element and I need to figure out how to get the ball to move differently depending on the surface it lies on. For instance, if the ball is hit on to the fairway, it will bounce and roll further than a ball that's hit in the rough.
My initial thought is to use raycasting to check the terrain texture beneath the ball. Once that terrain texture is determined, the rigid body settings would be changed accordingly. Is this even possible? If not, how else might I go about this? Any and all help would be greatly appreciated!! Thanks.
Answer by Owen-Reynolds · Sep 13, 2011 at 01:02 AM
Yes, you can read the weight of each terrain "splat texture" at a point on the ground.
You don't need a raycast, since you know the ball's x,z coordinate, you can directly look up terrain data for that spot (a raycast is only needed if you don't know whether the ground or something else is directly below you.)
Looking up the texture weights is a pain -- you have to convert from world x,z to splatMap x,z. The code looks like this (I've used it, works fine, but may be some pasting errors) http://answers.unity3d.com/questions/147282/detecting-what-parts-of-the-terrain-was-hit.html
The results you get are the 0-1 percent of each splat you painted with. In my case, I checked whether the grass splat was more than 1/2.