- Home /
Why is my quad not moving in sync with its collider?
I thought this might be due to an error in a custom controller I'm programming, but the problem still occurs when I try moving a Quad object with a BoxCollider2D component using a very simple script:
using UnityEngine;
public class Test: MonoBehaviour
{
private float speed = 1.5f;
private void Update()
{
float velocity = Input.GetAxisRaw("Horizontal") * speed * Time.deltaTime;
transform.Translate(new Vector2(velocity, 0));
}
}
Here's a screenshot of the issue: https://imgur.com/a/HE0dFie
As you can see, the BoxCollider2D and Quad mesh are not moving together. I have no idea why this is happening, but hopefully I'm just overlooking something.
Answer by Vega4Life · Nov 28, 2018 at 09:18 PM
Can you try using FixedUpdate() instead (as a test)? I have a feeling you won't see that issue.
I think problems can occur when using 2D colliders with 3D objects (a quad is 3D). You should just use a standard box collider here. Or, if you are making something 2D, just a simple square sprite instead of the quad.
Using FixedUpdate() does indeed work, but this is odd to me because I always use a Quad when prototyping in 2D, and it wasn't until this project that I noticed this issue.
It's probably better to do all my of movement in FixedUpdate() anyway.
Another interesting thing I just discovered is that adding a kinematic Rigidbody2D to my Quad fixes this issue, while allowing me to call my movement methods in Update or LateUpdate.
Interesting. Nevertheless, since you aren't moving your character via physics, I would keep your movement updates in Update(). If you do move your objects via rigidbody velocity or whatever, I would then use FixedUpdate();
Good luck