Search Unity

How can I temporarily have the player ignore colliding with any object and hold the player in place?

Discussion in 'Scripting' started by ChosenZeus, Jul 22, 2018.

  1. ChosenZeus

    ChosenZeus

    Joined:
    May 1, 2014
    Posts:
    18
    I want to be able to set the player coordinates to an object but not have to worry about turning off the collider. on that object as other colliders may be near currently the player get shoved around after the position and rotation set on the next frame sometimes. it would make life easier if I could set the players collider off and freeze him possible in space above ground so he doesn't fall through.

    Been having a heck of a time finding let alone manipulating the player collider.

    I'm working in C#

    Thanks!!!
     
  2. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824
    You can try to add a layer and configure the physics collision matrix (in the Physics settings) to ignore collisions with everything that shouldn't collide with it anymore.

    Instead of diabling or removing the collider, you can then change the layer mask to the new one and keep the collider for certain things. You still have to change it via code then, just like disabling the collider, but you can keep the collider in order to have other things still running (such as raycasts, if the layer is included in the specified layer mask).
     
  3. ChosenZeus

    ChosenZeus

    Joined:
    May 1, 2014
    Posts:
    18
    That seems like a lot, and for sure new stuff for me to learn, which is fine :) in the meantime do you know how to access the collider on a player object I don't see one in it's objects? I was also thinking I could add a ridgidbody and make this kinematic for the freezing. Just trying to do this simply for concept testing, I will for sure look into the layer stuff new to me on that one.
     
  4. ChosenZeus

    ChosenZeus

    Joined:
    May 1, 2014
    Posts:
    18
    As far as layers, I fell through the floor (which is a good thing in this case :) by setting default OFF in physics after creating a new one, so good on you for that, I find the layers thing a bit confusing as of now, anything in there to ignore gravity and leave the player position as it is?

    if (Input.GetKeyDown (KeyCode.Escape)) {
    GameObject.Find ("Capsule").layer = LayerMask.NameToLayer ("NoClip");
    }

    Worked for bypassing colliders, now just need to solve the gravity issue. I am trying to position a player by code which may have the player partially combined with an object, think seating the player or putting the player on something.
     
    Last edited: Jul 22, 2018