I’m having a weird issue, and I noticed it in Glitch Garden as well – where objects will go one unit too far before registering collisions. When moving right (x = 1) everything works as it should, but when moving left (x=-1) it goes one tile too far. Screenshots below. Here’s the code:
public class EnemyMovement : MonoBehaviour
{
[SerializeField] float moveSpeed = 3f;
[Header("Collision Detection")]
[SerializeField] Rigidbody2D myRigidbody;
[Tooltip("Collider for dealing with player collisions")]
[SerializeField] Collider2D myPlayerCollider;
[Tooltip("Collider for finding platform end")]
[SerializeField] Collider2D myEdgeCollider;
Vector2 originalPos;
void Start()
{
originalPos = transform.position;
}
// Update is called once per frame
void Update()
{
ProcessMove();
}
private void OnTriggerExit2D(Collider2D collision)
{
transform.localScale = new Vector2(-(Mathf.Sign(myRigidbody.velocity.x)), 1f);
}
private void ProcessMove()
{
if (IsFacingRight())
{
myRigidbody.velocity = new Vector2(moveSpeed, 0f);
}
else
{
myRigidbody.velocity = new Vector2(-moveSpeed, 0f);
}
transform.position = myRigidbody.position;
}
private bool IsFacingRight()
{
return transform.localScale.x > 0;
}
}