Search Unity

  1. Improved Prefab workflow (includes Nested Prefabs!), 2D isometric Tilemap and more! Get the 2018.3 Beta now.
    Dismiss Notice
  2. The Unity Pro & Visual Studio Professional Bundle gives you the tools you need to develop faster & collaborate more efficiently. Learn more.
    Dismiss Notice
  3. Let us know a bit about your interests, and if you'd like to become more directly involved. Take our survey!
    Dismiss Notice
  4. Improve your Unity skills with a certified instructor in a private, interactive classroom. Watch the overview now.
    Dismiss Notice
  5. Want to see the most recent patch releases? Take a peek at the patch release page.
    Dismiss Notice

Player Not Colliding with Walls

Discussion in 'Physics' started by GameNate, Jun 15, 2018.

  1. GameNate

    GameNate

    Joined:
    Jun 15, 2018
    Posts:
    7
    In my Unity project, I have a 3rd person player. The actual model of the player is the child of a parent named
    Player, which is just a placeholder. Everything works perfectly with the player camera-wise, but whenever I walk into walls, the player just walks through them instead of colliding with them.

    The actual player model and the Player parent have rigidbody (since they move), and the Player parent has a capsule collider. The walls that the player walks through just have regular box colliders. No matter what I do, I cannot seem to get a collision to stop the player from walking through the walls!

    Does anyone have any ideas on how to fix this issue?
     
  2. dgoyette

    dgoyette

    Joined:
    Jul 1, 2016
    Posts:
    668
    So, you have a parent object with the capsule collider, which contains a child object that has the rigidbody and the model? If so, the rigidbody is going to move around and leave the collider behind. Try putting the rigidbody on the parent object.

    If that doesn't work, did you put any of the objects on different layers? (I'd assume they're all on the default layer, unless you did something to change that.)
     
  3. GameNate

    GameNate

    Joined:
    Jun 15, 2018
    Posts:
    7
    I took the rigidbody off of the child object (the player model) and put it on the parent object. That did not end up working. I am not sure why I am encountering this issue because the walls just have normal box colliders.

    Everything is on the default layer. I'm not experienced enough to mess around with layers.
     
  4. GameNate

    GameNate

    Joined:
    Jun 15, 2018
    Posts:
    7
    Capture1.JPG Capture2.JPG Capture3.JPG

    All of the walls are duplicates of each other, so they have the same components with the same values.
     
  5. dgoyette

    dgoyette

    Joined:
    Jul 1, 2016
    Posts:
    668
    Well, you're presumably not falling through the floor, right? So the floor collider works, but the wall collider doesn't?

    I don't know how complex your scene is, but you could try exporting it and attaching it to this issue (if it's not too large). Right click the scene object in your project explorer, and choose "Export Package".
     
  6. dgoyette

    dgoyette

    Joined:
    Jul 1, 2016
    Posts:
    668
    Thanks for the screenshots. I don't see any controllers on your character. How are you moving it around? What force is pushing the character through the walls?
     
  7. dgoyette

    dgoyette

    Joined:
    Jul 1, 2016
    Posts:
    668
    Ah, I bet your just have a "walk" animation that moves the model around. But since the model is a child of the collider, when it moves it won't move the collider with it.

    If you're going to use a root motion animation on your character, the model and the animator probably need to be on the parent object. The collider can go on it too, or you can put the collider on a child object. Have you tried not breaking your player into parent/child objects? Have you tried putting them all on the same object?
     
  8. GameNate

    GameNate

    Joined:
    Jun 15, 2018
    Posts:
    7
    I have not tried that, but I will do that now. And yeah, I think what I ended up doing was having animations play, and the animations weren't attached to anything.

    It seems that even when the player model is by itself and contains a Mesh Collider and rigidbody, it still walks through the walls. My project is small, so I will try to attach it to a reply.
     
  9. GameNate

    GameNate

    Joined:
    Jun 15, 2018
    Posts:
    7
    The file is too large, it won't attach.

    I only recently started getting back into Unity, so I've forgotten a lot of things. Is there a way to create a Third Person Controller for my character, without the animations that it already has on it?
     
  10. dgoyette

    dgoyette

    Joined:
    Jul 1, 2016
    Posts:
    668
    There are a few different approaches. Probably the easiest thing is just to use what's built into Unity's Standard Assets (Assets -> Import Package -> Characters). That will put a Standard Assets folder in your project which contains a ThirdPersonController prefab. You'd then tweak it by switching out the model with your own (or just use the model is has if you don't care.)

    What gets tricky is when you start using root motion animations. The basic idea is that an animation in Unity has the option to move the actual game object's position, or to leave the position unchanged. For example, with a "walking" animation, you can decide whether the animation should be in-place (the character's legs move, but the character doesn't go anywhere), or whether the animation should apply root motion (where the walking animation actually moves the character forward in the world). For the first approach, you'd need to handle the actual position change in some way yourself.

    I personally find root motion pretty complex to deal with, but that probably because I haven't used it much.
     
  11. GameNate

    GameNate

    Joined:
    Jun 15, 2018
    Posts:
    7
    When I change the settings of the ThirdPersonController prefab, I switch the model and the animator. The original animator won't work because my character is too large compared to the original Ethan model, so his arms and legs mesh into each other if the animator stays the same.

    When I do this, and I try to play my game, the character model goes crazy and won't move like it's supposed to. The colliders won't work either, even with the original settings the same.

    I have all of the animations that I want to play when the player model walks, as well as the idle animation. Is there another way I can put these into the game and have the colliders for my player model work?
     
  12. dgoyette

    dgoyette

    Joined:
    Jul 1, 2016
    Posts:
    668
    Maybe someone else who has built a root-motion character animator can chime in, as I haven't done that. I'm not sure how you get the animation to stop applying root motion in cases where the character is hitting a wall, for example.

    The approach I'd personally take is to turn off root animation on the animator, so that the animations don't change the player's position, and handle all motion through the rigidbody. You can look at the 3rd Person controller script in Standard Assets, and maybe just pull out the parts responsible for moving the rigidbody around. But then you'd still need to write up calls to the Animator to get it to play the right animations at the right time based on what the rigidbody is doing.

    Honestly, getting motion and animation working really nicely together isn't simple. Most people start out with just a plain old capsule as their player, with no animations, until they get some of the gameplay of their game working. The character model and animations are icing.