Monday, April 13, 2015

How does Unity's Collider.Bounds work?


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().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?



Answer



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.


No comments:

Post a Comment

Simple past, Present perfect Past perfect

Can you tell me which form of the following sentences is the correct one please? Imagine two friends discussing the gym... I was in a good s...