Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We’re making changes to the Unity Runtime Fee pricing policy that we announced on September 12th. Access our latest thread for more information!
    Dismiss Notice
  3. Dismiss Notice

Question Is this an efficient way to determine surface beneath player?

Discussion in 'World Building' started by henners999, Apr 27, 2023.

  1. henners999


    May 30, 2020
    Is it efficient to send a raycast down from a wheel into a plane to query the colour of the texture? This is how I'm currently doing it and it works well. Someone has suggested I put the data from the textures in an array and query that as the car object moves.

    I'm using texture2D.GetPixel

    Is this a good way to do it?
  2. ciaranhappy1


    Oct 22, 2018
    Raycasting down certainly makes sense, but i would be cautious about calling GetPixel() that often. Its not amazingly slow or anything, but it is slow enough that I've had to optimize when its called for performance reasons in my own project. I dont know how fast your car is, but ive found even in situations where calling it every frame makes apparent sense, you really only need to call it every few frames.
    If your not calling this that often, then its fine since GetPixel, while a little slow, is still really quick from a human perspective. It just starts to stack up if your doing it constantly.

    As for whether GetPixel() is the best way to get the data you want from the enviroment... thats hard to answer without knowing what it is your trying to do here. I'm personally using physics materials to determine what a surface physically is, which is works well enough. But Im also using GetPixel() to find out how well lit an area is. The best solution is going to always depend on your problem. I dont know what your problem is, because you havent told me.

    As for putting the data in an array - thats how this sort of thing would usually be done. This is very context dependent though, and I dont know *why* you need data from the ground. If a color is all you need then an array might not be necessary, but if your getting a color and extrapolating a bunch of extra information from it, then you should probably put that data in an array object and compare instead of calculating on the fly.

    overall though, what your doing is probably fine.

    Also, im not sure the World Building subforum is the optimal place for a question like this. Scripting might have been a better place.
    Last edited: May 12, 2023
    henners999 likes this.