1
\$\begingroup\$

From waht I understand, Collider.Bounds are the sides or bounds of the collider of the GameObject (at least for a cube with a BoxCollider). But I got a result that I wasn't expecting when I tried moving a cube continuously downwards and drawing the bounds with lines. While moving frame by frame, the collider bounds seemed to be not bounding the cube (or at least that's what my test is showing, if my test is correct).

Here's 3 frames of screenshot:

enter image description here enter image description here enter image description here

What I did for this is create a new 3D project, add a Cube, then added a single script and then added this script to the Cube. The whole script is:

using UnityEngine;

public class NewBehaviourScript : MonoBehaviour
{
    Vector3 velocity;

    // Start is called before the first frame update
    void Start()
    {
        velocity = Vector3.zero;
    }

    // Update is called once per frame
    void Update()
    {
        velocity.y += -0.5f;
        transform.Translate(velocity);

        Bounds bounds = GetComponent<Collider>().bounds;
        Debug.DrawLine(new Vector3(bounds.min.x, bounds.min.y, bounds.min.z), new Vector3(bounds.max.x, bounds.min.y, bounds.min.z), Color.red);
        Debug.DrawLine(new Vector3(bounds.max.x, bounds.min.y, bounds.min.z), new Vector3(bounds.max.x, bounds.max.y, bounds.min.z), Color.red);
        Debug.DrawLine(new Vector3(bounds.max.x, bounds.max.y, bounds.min.z), new Vector3(bounds.min.x, bounds.max.y, bounds.min.z), Color.red);
        Debug.DrawLine(new Vector3(bounds.min.x, bounds.max.y, bounds.min.z), new Vector3(bounds.min.x, bounds.min.y, bounds.min.z), Color.red);
        Debug.DrawLine(bounds.min, bounds.max, Color.red);
    }
}

Am I not understanding something or am I doing something wrong?

\$\endgroup\$
6
  • 1
    \$\begingroup\$ I notice you're moving this collider with the object's Transform component, rather than using the physics system that's responsible for managing colliders. Have you tried notifying the physics system that you've moved its stuff using eg. Physics.SyncTransforms or keeping the movement in the control of the physics system throughout? \$\endgroup\$
    – DMGregory
    Commented Jun 17, 2019 at 22:59
  • \$\begingroup\$ @DMGregory I believe that is the answer OP is looking for :) \$\endgroup\$
    – Alex F
    Commented Jun 17, 2019 at 23:34
  • \$\begingroup\$ @DMGregory: Ok, I guess I'm misunderstanding something. I thought Physics only applies when your GameObject has a RigidBody attached? And I thought the Collider is just a property that always bounds the object and is only detected/used by Physics when there's a RigidBody as well. If that's not the case, then maybe I shouldn't use Bounds to draw the lines. \$\endgroup\$
    – g_b
    Commented Jun 17, 2019 at 23:51
  • 1
    \$\begingroup\$ You should use colliders when you want physics collisions, including physics.raycast, etc. Everything involving colliders is a physics effect, even if you don't use rigid body dynamics. If you just want the bounds of the visible object, you can use renderer.bounds instead. \$\endgroup\$
    – DMGregory
    Commented Jun 17, 2019 at 23:53
  • \$\begingroup\$ OK, thank you. Also, your suggestion Physics.SyncTransforms worked. I would like to mark it as the answer if you'll put it as an answer. \$\endgroup\$
    – g_b
    Commented Jun 17, 2019 at 23:55

1 Answer 1

1
\$\begingroup\$

As @DMGregory stated, I needed to notify the Physics system by using Physics.SyncTransforms() after every call to Transform.Translate() and the Collider lined up perfectly after that.

\$\endgroup\$
1
  • 1
    \$\begingroup\$ Note that if a collider does not have a Rigidbody attached, the physics system assumes it will be static, and makes optimizations accordingly. Moving such a collider with its transform can be more expensive, since it can force the physics engine to re-apply these optimizations it thought it would only have to do once for an unmoving object. If you're going to move a collider, it's often better to attach a kinematic rigidbody and move it with that, so the physics engine can see what you're trying to do. \$\endgroup\$
    – DMGregory
    Commented Jun 19, 2019 at 3:08

You must log in to answer this question.

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