I'm trying to make a 3D game with unity using the character controller and I'm just parenting the model to it and rotating only the model when moving for my character.

The problem is that I want to programmatically load in this setup by creating the capsule etc. with code, but by default when my mesh is loaded, it's halfway offset, so I try to move it down by half of the capsule's height, but for some reason the height I'm getting from the renderer or capsule collider bounds is always a little too big and half of the height is always a little BELOW the bottom of the capsule collider.

This can be seen by just me testing to position a small cylinder (was supposed to be the new origin of the capsule to get the origin to be at the bottom, but that's another story), and I set it to EXACTLY HALF OF THE HEIGHT but for some reason it's a little bit lower, here's the pic:

enter image description here

as you can see, the cylinder is for some reason a little bit BELOW the bottom of the capsule, but I want it to be exactly at the bottom, so it can be the new origin (and so I can position the player on top of things really easily).

Here's the code I use to generate the player, capsule, and new origin:

    void makePlayer()
    GameObject player = GameObject.CreatePrimitive(PrimitiveType.Capsule);
    player.name = "Player";

  //      player.GetComponent<MeshRenderer>().enabled = false;//.material.color = new Color(1f, 0.5f, 0.6f, 1f);
        player.GetComponent<CharacterController>().radius = 0.3f;
     //   player.tag = "Player";
        GameObject mesh = Resources.Load<GameObject>("models/chassid");
        RuntimeAnimatorController playerMov = Resources.Load<RuntimeAnimatorController>("animation controllers/RUNNING");

    GameObject chassid = Instantiate(mesh);
    chassid.name = "chassid";

    chassid.GetComponent<Animator>().runtimeAnimatorController = playerMov;
    chassid.transform.GetChild(1).tag = "Player";

    string pathToMaterial = "Materials/playerMat";
    Material playerMat = Resources.Load<Material>(pathToMaterial);
    chassid.transform.GetChild(1).GetComponent<SkinnedMeshRenderer>().material = playerMat;

    Vector3 capsuleSize = player.GetComponent<Renderer>().bounds.size;
    Vector3 chassidSize = chassid.GetComponentsInChildren<Renderer>()[0].bounds.size;
    chassid.transform.parent = player.transform;
    Vector3 tempPos = chassid.transform.localPosition;

    tempPos.y -= (chassidSize.y / 2);// + chassidSize.y / 12;

    chassid.transform.localPosition = tempPos;
    GameObject origin = GameObject.CreatePrimitive(PrimitiveType.Cylinder);
    origin.transform.localScale = new Vector3(0.1f, 0.1f, 0.1f);
    origin.transform.position = player.transform.position - new Vector3(0,player.GetComponent<CapsuleCollider>().bounds.size.y / 2,0);
    player.transform.parent = origin.transform;
    GameObject tempCam = GameObject.Find("Main Camera");
    GameObject mainCamera;
    if(tempCam == null)
        mainCamera = new GameObject("Main Camera");
    } else
        mainCamera = tempCam;
    mainCamera.tag = "MainCamera";

I guess the problem is in bounds.size, apparently it's not giving the accurate size for capsule meshes?


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.