Search Unity

Resize Characterl Controller height Problem

Discussion in 'Scripting' started by PavolM, Mar 23, 2013.

  1. PavolM

    PavolM

    Joined:
    Sep 4, 2012
    Posts:
    37
    Hi everyone

    As i mentioned in a title i have problem with resizing character controler.
    Its ok when i scale it down but when i scale it back to original ( bigger size ) it fall throught my ground collider is that any easy solution how to do this without falling throught?

    Thanks :)
     
  2. stridervan

    stridervan

    Joined:
    Nov 11, 2011
    Posts:
    141
    because the scaling happens from the center, when you make it smaller the extent is less than what it was before but when you make it bigger the lower extent falls below the lowest terrain height. To fix this you also need to adjust the y position of the controller when u scale it. Two things that should help you.

    Code (csharp):
    1.  
    2. Terrain.activeTerrain.SampleHeight(Vector3); // Gets the terrain height at a certain coordinate
    3. GameObject.collider.bounds.extent // the extension of a collider from the center
    4.  
    5. /* So basically you can do this */
    6. var oldPosition = GameObject.transform.position;
    7. var height = Terrain.activeTerrain.SampleHeight(oldPosition);
    8. height += GameObject.collider.extent.y * 2; // assuming the pivot is at the center
    9. GameObject.transform.position = new Vector(oldPosition.x, height, oldPosition.z);
    10.  
    If the pivot point is at the center you may need the extent adjustment, however if it is at the bottom(i always set the pivot at the bottom for every models i create to avoid the problem) you do not need to compensate for the extent.
     
  3. PavolM

    PavolM

    Joined:
    Sep 4, 2012
    Posts:
    37
    thanks, i get the idea it should work now :)