So, I have two players when I first made them they were with the default size Y = 1. After I finished the scripts I wanted to change their size but when I did that, the calculations use the size of Y = 1 and I don't understand why is that.

The code I am running is this:

public class Player1Controller : PlayersController {
    private Vector3 downMost;
    private Vector3 upMost;
    private float minY, maxY;
    private float halfHeight;

    // Start is called before the first frame update
    private void Start () {
        halfHeight = GetComponent<SpriteRenderer> ().sprite.bounds.extents.y;

        float distance = transform.position.z - Camera.main.transform.position.z;
        downMost = Camera.main.ViewportToWorldPoint (new Vector3 (0, 0, distance));
        upMost = Camera.main.ViewportToWorldPoint (new Vector3 (1, 1, distance));

        minY = downMost.y + halfHeight;
        maxY = upMost.y - halfHeight;

    // Update is called once per frame
    private void Update () {
        float verticalMovement = Input.GetAxis ("Player1Vertical");
        MovePlayer (verticalMovement, transform);
        PlayerLimit (transform, minY, maxY);

The PlayerLimits method is

public void PlayerLimit (Transform transform, float minY, float maxY) {
        float restrictY = Mathf.Clamp (transform.position.y, minY, maxY);
        transform.position = new Vector3 (transform.position.x, restrictY, transform.position.z);

I assume the problem is either in the sprite bounds.extents.y or the restrictY but I don't get it.

This helps me with the Player limits, shown in the next image: Limit of the players

As you can see on the left side is the modified player with size less than 1 and on the right side is the player with the default size, the script is the same but the calculations are wrong. The left bar had to go till the end of the border but it doesnt.

  • \$\begingroup\$ Please post your code as text, not an image, and tag your question correctly. This code is in C#, not the deprecated language UnityScript. \$\endgroup\$
    – DMGregory
    Oct 22, 2019 at 21:08
  • \$\begingroup\$ Sorry for the misunderstanding this is my first time asking a question here. \$\endgroup\$ Oct 22, 2019 at 22:36

1 Answer 1


Collider boundaries extents do not scale with the Transform. The GameObject / MeshRenderer's role is to give a visual shape to your object while the Collider's role is to give it a physical shape in the Physics world. If one aspect (either visual or physical) changes it doesn't have an impact on the other one. I agree that the fact the Collider automatically tries to fit a Mesh bounds when you add it to a GameObject the first time can be missleading.

TL;DR: if you need to get the right size of the Collider bounds extents, simply scale them accordingly to transform.localScale. Something like:

Vector3 extents = _my_collider.bounds.extents;
extents.x *= _my_transform.localScale.x;
extents.y *= _my_transform.localScale.y;
extents.z *= _my_transform.localScale.z;

side note: it's simple when a scale is applied to your game object only... but can get messier if your game object parents are scaled too. Then you'll have to loop in the hierarchy from root to your game object and combine every scaling value


You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .