Search Unity

Modifying Bone Scaling

Discussion in 'Scripting' started by MrZeker, Apr 10, 2019.

  1. MrZeker

    MrZeker

    Joined:
    Nov 23, 2018
    Posts:
    227
    Hello im trying to figure out how to modify the scale of a certain bone with scripting. What i want to do is essentially that if a bool becomes true, then "BoneX" will increase its scaling in X and Z, but not in Y. i havent found any command or any way to scale bones in unity and google searchs hasnt provided any answers other than scaling bones its the best practice, but for now its what i need.
     
  2. adreamvoyager_unity

    adreamvoyager_unity

    Joined:
    Oct 16, 2018
    Posts:
    14
    You could try something like this. Then simply reference one of the bones in the inspector.

    For constant scaling.


    Code (CSharp):
    1. public GameObject bone;
    2. public float ScaleRate = 2;
    3. bool scale = false;
    4.  
    5. void Update()
    6. {
    7.  
    8. if (scale)
    9. {
    10. Vector3 boneScale = bone.transform.localScale;
    11. boneScale.x += ScaleRate * Time.DeltaTime;
    12. boneScale.z += ScaleRate * Time.DeltaTime;
    13. bone.transform.localScale = boneScale;
    14.  
    15. }

    Alternatively if you want to simply increase the scale a certain amount you could do something like this:



    Code (CSharp):
    1. public GameObject bone;
    2. public float scaleSizeX, scaleSizeZ, originalSizeX, originalSizeZ;
    3. bool scale = false;
    4.  
    5. void Start()
    6. {
    7.  
    8. originalSizeX = bone.transform.localScale.x;
    9. originalSizeZ = bone.transform.localScale.z;[/INDENT]
    10.  
    11. }
    12.  
    13. void Update()
    14. {
    15.  
    16. if (scale)
    17. {
    18. Vector3 boneScale = bone.transform.localScale;
    19. boneScale.x += scaleSizeX;
    20. boneScale.z += scaleSizeZ;
    21. bone.transform.localScale = boneScale;[/INDENT]
    22.  
    23. }
    24. else
    25. {
    26. Vector3 boneScale = bone.transform.localScale;
    27. boneScale.x += originalSizeX;
    28. boneScale.z += originalSizeZ;
    29. bone.transform.localScale = boneScale;
    30.  
    31.  
    32. }
    33.  
    34.  
    Did that help?
     
    Last edited: Apr 10, 2019
    MrZeker likes this.
  3. MrZeker

    MrZeker

    Joined:
    Nov 23, 2018
    Posts:
    227
    Nice, im gonna try it when im home, i think this is exactly what i needed!! thanks.
     
  4. JointWater

    JointWater

    Joined:
    Sep 14, 2019
    Posts:
    6