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. Voting for the Unity Awards are OPEN! We’re looking to celebrate creators across games, industry, film, and many more categories. Cast your vote now for all categories
    Dismiss Notice
  3. Dismiss Notice

How to just center a mesh properly in a charactercontroller?

Discussion in 'Scripting' started by cobykaufer, Aug 8, 2018.

  1. cobykaufer

    cobykaufer

    Joined:
    Aug 8, 2018
    Posts:
    6
    Hi I have a simple character mesh and it's parented to my character controller-invisible-capsule object and all the moving code is applied to the parent (if that's relevant), but the point Is I'm trying to line up the mesh to be exactly inside the capsule to make it look like the player is walking ABOVE the ground (pretty normal ...?) So by default the character controller is origined in the center so I move it up by half of it's height to try to get it to line up with the character mesh, which has it's origin below it's feet a little, and this is the result: ![alt text][1]


    as you can clearly see, his feet slightly go into the ground, I have no idea why this is happen, then should be just above the ground like any other normal feet! Here's the code for setting the capsule up half of it's width, since it starts by lining up the mesh in it's CENTER for some reason:

    Vector3 tempPos = player.transform.position;

    tempPos.y += (capsuleSize.y / 2);// + chassidSize.y / 12;
    chassid.transform.position = player.transform.position;
    player.transform.localPosition = tempPos;
    chassid.transform.parent = player.transform;




    and here's a picture of the mesh (which is just a blender file, easier for import):
    ![alt text][2]


    [1]: https://image.ibb.co/fJHDRK/problem.png
    [2]: https://image.ibb.co/eHMCLe/problem2.png


    As you can see, in the blender file, the origin is slightly below the characters feet, IN THE CENTER, so I don't know why it's not working as planned.

    Also, you can see in the first picture that he's slightly moved to the center of the controller on the z(?) axis, but I want him to be completely centered in, any idea how to fix this?
     
  2. Scabbage

    Scabbage

    Joined:
    Dec 11, 2014
    Posts:
    268
    Instead of trying to align it all programmatically why not just create a player prefab and make sure it's all lined up in the editor before even hitting play? Then you wouldn't need to arrange transform components in code.
     
  3. cobykaufer

    cobykaufer

    Joined:
    Aug 8, 2018
    Posts:
    6
    Then how would I 1) dynamically change the model or 2) dynamically change the size of the model? I want to be able to automatically just line up the character controller with the mesh
     
  4. cobykaufer

    cobykaufer

    Joined:
    Aug 8, 2018
    Posts:
    6
    BTW I just tested it with exporting the model as an FBX and it works perfectly, looks like it's a bug with blender files?