I’m working on a game where my game objects have a set limit to how far they can move. I need to be able to track when they have entered a different terrain type ( like plains → woods ) and decrease their total movement accordingly. My only idea is to create invisible game objects for the terrain that would detect collision when the moving object enters it, but I was wondering if anyone had a better solution.
Thanks.
You can use a texture and raycasting. The idea is to create a texture sitting on a plane that “overlays” the terrain. You can cast rays against the plane and get the texture coordinate. You can then use the color value at the coordinate to determine the property of the terrain. That is the Texture is blobs of color and/or alpha that represent each of the movement types. A few notes:
- The texture would not be visible (renderer would be disabled).
- The texture could sit below the terrain or above the character.
- You can use Collider.Raycast() (rather than Physics.Raycast()) to cast against just that plane.
-
RaycastHit.textureCoord refrence page show how to take a hit and translate it to a SetPixel. You will want a GetPixel() but the the addressing is the same. Also most of the code in the example would be either unncessary or should be run in Start().
- The texture will need to be Read/Write enabled.