- Home /
Is Physics2D BoxCast accuracy influenced by tranform.scale ?
Replication steps
Create two empty game objects with any sprite.
Scale should be (1,1,1) by default.
Add a BoxCollider2D to each of the game object.
Add following
Mover.cs
script to them and assign their own BoxCollider2D to the property.Drag one game object to the left and one to the right. y axis values are the same. Assign 1 as the X_dir property to the object on the left and -1 to the object on the right, i.e. let them run toward each other.
In
Edit / Project Settings / Physics 2
, turn offQueries Start In Colliders
Script
public class Mover: MonoBehaviour
{
public BoxCollider2D boxCollider;
public float x_dir;
protected virtual void Start(){
boxCollider = GetComponent<BoxCollider2D>();
}
protected void FixedUpdate()
{
UpdateMotor(new Vector3(x_dir, 0,0));
}
protected virtual void UpdateMotor(Vector3 input)
{
Vector3 delta = new Vector3(input.x * Time.deltaTime, input.y * Time.deltaTime, 0);
RaycastHit2D hit = Physics2D.BoxCast(transform.position, boxCollider.size, 0, new Vector2(0, delta.y), Mathf.Abs(delta.y));
if (hit.collider == null) {
// Make this thing move
transform.Translate(0, delta.y, 0);
}
hit = Physics2D.BoxCast(transform.position, boxCollider.size, 0, new Vector2(delta.x, 0), Mathf.Abs(delta.x));
if (hit.collider == null) {
// Make this thing move
transform.Translate(delta.x, 0, 0);
}
}
}
Expected Behaviour
The two game objects will be blocked and stop before colliding into each other.
The problem
If we change any of the game objects' scale to , say (2,2,1), they will run over each other and continue.
Question
Does this imply the method Physics2D.BoxCast
is inaccurate with scale changed?
Your answer
![](https://koobas.hobune.stream/wayback/20220613045747im_/https://answers.unity.com/themes/thub/images/avi.jpg)