Search Unity

How to change collision response of a specific object at runtime?

Discussion in 'Scripting' started by Paulx774, Sep 9, 2021.

  1. Paulx774

    Paulx774

    Joined:
    Mar 18, 2021
    Posts:
    103
    Is there a way change AN OBJECT's collision response against a specific layer? For example; when a car in my game goes faster than 40 KPH, I want the car to ignore a specific layer in terms of physics. When the car's speed lower than 40 KPH, I want the car to interact with the layer. There will be more than one car in the game. So, each other needs to change their responses depending on their speeds. Is that possible?
     
  2. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,459
    Yes, write a script to look at the speed and change the collision layers appropriately. There's not going to be a stock function for this because it's a very specific need for your project.
     
  3. Paulx774

    Paulx774

    Joined:
    Mar 18, 2021
    Posts:
    103
    "changed the collision layers appropriately". Does it mean I should use Physics.IgnoreLayerCollision function? Doesn't it change the collision response of all the related objects?

    In my case, a specific object's collision response should be changed. For example; there are 2 cars in my scene. One is going above 40 KPH, the other is going at 30 KPH. Only the collision response of the car that's going above 40 KPH should change, not the other's, and the both have the collision layer of "Car".
     
  4. Neto_Kokku

    Neto_Kokku

    Joined:
    Feb 15, 2018
    Posts:
    1,751
    You create a different layer with the different response settings, then switch the car's layer to that depending on the velocity.