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. Dismiss Notice

Using Splatmaps To Change A Terrains Properties For A Vehicle Simulation

Discussion in 'Scripting' started by Littlefysh, Mar 20, 2020.

  1. Littlefysh

    Littlefysh

    Joined:
    Feb 17, 2020
    Posts:
    5
    Hi everyone,

    I realise there's a lot of questions about splatmaps out there but I've spent roughly a week searching and can't find the answer I need. I'm resorting to this because it's holding up my entire project and is frustrating me to no end.

    Essentially what I want to do is hijack the raycast in each WheelCollider of a vehicle and use that raycast to determine what type of terrain is below the wheel. I want to change the grip of the material below the wheel. I believe this is possible in Unity using splatmaps, I'm just struggling with the 'logistics' I suppose.

    Questions like "Do I start in a script attached to the terrain or to the vehicle controller?" are what I'm struggling with.

    I've seen this done with conventional humanoid characters, using surfaces like ice as an example, but I just can't wrap my head around doing it with a vehicle.

    Any help is appreciated, thanks for your time.
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,762
    Since all wheels might contribute to the behavior of the vehicle, to keep yourself sane, you probably want these two things:

    - an individual sensor script on each wheel that reports to its own internal variable that it is on terrain type X

    - a single vehicle behavior modifier that has references to all of those sensors and acts accordingly to what they have sensed.

    For instance, if any wheel sensor goes into lava, the vehicle behavior should be to explode

    But if any sensor just gets onto ice, the vehicle might only lose a fraction of its traction. If they're all on ice, you completely lose steering control.

    Also, for reading off the terrain, there's a script going around called TerrainDetector (probably several versions of it) that tells you what you're walking on, at least an index into the splatmap values IIRC.
     
  3. Littlefysh

    Littlefysh

    Joined:
    Feb 17, 2020
    Posts:
    5
    Thankyou.

    Is it possible to do what TerrainDetector does but myself? I need to avoid using other peoples resources. Using plugins and other peoples scripts doesn't help me unfortunately.
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,762
    Of course! It's just data access.
     
  5. Littlefysh

    Littlefysh

    Joined:
    Feb 17, 2020
    Posts:
    5
    So this would be a separate script? What object would it be attached to? I'm really struggling with the architecture of the solution.
     
  6. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,762
    If there's only one terrain, put it on there, then find it at Start() time and use it.

    If there's multiple terrains in a single scene, you might need a terrain manager to decide which terrain to go snooping in.

    The beautiful thing about software is that it is SOFT... try some stuff out, test it, if it doesn't do what you need, you have learned something and you can easily rework it.
     
    Littlefysh likes this.