Search Unity

Prevent CharacterController from pushing rigidbodies

Discussion in 'Scripting' started by Nition, Apr 5, 2017.

  1. Nition

    Nition

    Joined:
    Jul 4, 2012
    Posts:
    781
    There are a lot of previous threads about CharacterControllers not being able to push rigidbodies, or about rigidbodies pushing CharacterControllers. I've got the opposite problem: I want to stop my CharacterController from being able to push rigidbodies around.

    When a rigidbody is still, a CharacterController cannot move it at all. But once rigidbodies get up to a certain speed, "pushing" on them with a CharacterController starts being able to move them around. And the movement they get from "pushing" is super janky and not preventable by things like increasing the mass. It seems more like an unintended quirk of the physics engine (as of Unity 5.6), like the object is just trying to vacate the space the CC is taking up. This is a basic CharacterController without a rigidbody or other colliders on it.

    I'd much rather that all rigidbodies just ignored my CharacterController object entirely, and the CC dealt with keeping out of their way.

    Steps to reproduce:
    - Have a big rigidbody object, any mass
    - Have a basic CharacterController object with movement controls
    - Push on the stationary rigidbody with the CC. It won't budge
    - Have something else get the big rigidbody moving
    - Try pushing on it while it's still moving. Suddenly the CC has the power to move it
     
  2. Laperen

    Laperen

    Joined:
    Feb 1, 2016
    Posts:
    1,065
    have you tried using collision layers under physics?
     
  3. Nition

    Nition

    Joined:
    Jul 4, 2012
    Posts:
    781
    Physics collision layers might be perfect if they could be one-way, but they're always two-way as far as I know. So disabling rigidbody collisions with the CC also lets the CC go through the rigidbody. To be fair a "one-way" physics collision doesn't make much sense. But that is kind of what I need: Rigidbody ignores CharacterController but CC gets out of the way of the rigidbody.

    I could always write custom code to keep the CC from colliding manually of course, but that's reinventing the wheel and it'd probably be worth using a whole different system if it comes to that point. Like a true rigidbody/physics-based character.
     
    Last edited: Apr 5, 2017
  4. Nition

    Nition

    Joined:
    Jul 4, 2012
    Posts:
    781
  5. GlitchCrew

    GlitchCrew

    Joined:
    Oct 13, 2016
    Posts:
    2
    I had exactly the same problem, thank you for this solution!
     
    Last edited: Nov 3, 2017
    Nition likes this.
  6. CrazyRocksStudios

    CrazyRocksStudios

    Joined:
    Sep 29, 2015
    Posts:
    15
    Disabling collisions for character controller makes it behave strange when some rigid body hits it - in my case cc was suddenly pushed on the top of a car that hit the player. This was not acceptable in my case.

    It had a problem where player could push around vehicles that were moving at certain speed.
    The workaround that works perfect for this case is following:
    1. Disable collisions between player and vehicle
    2. Create copy of vehicle collider that is not in vehicle hierarchy (to avoid being controlled by rigid body) - this is only car vs player collider. In my case this collider is a rigid body car sibling under vehicle root.
    3. This collider needs to have simple copy transform script (e.g. void LateUpdate(){transform.pos =obj.pos; transform.rot = obj.rot})

    Now player can't push vehicles (rigid bodies) at all - but still can be pushed by them ! Note that player movement while being pushed will not be fluent but in my case this was not a problem.
     
    RuNix likes this.
  7. rasmuskoue0

    rasmuskoue0

    Joined:
    Sep 23, 2019
    Posts:
    1
    CrazyRocksStudios
    Thank you so much, this solved my problem :D
     
  8. Osegbu

    Osegbu

    Joined:
    Apr 18, 2023
    Posts:
    1
    Hi, I couldn't get the vehicle to push the Character Controller... In my case the character controller was push on top the vehicle.