I am having a square sprite and attached rigidbody2d and box collider to it and a simple character controller which checks grounded with physics2d.raycast function. When i play, the sprite moves, but after sometime it lags or to say it stops moving for few seconds and again it move. But instead of box collider i used circle collider that lag problem didnot appear. Why? Could someone explain. Also what is interpolate in rigidbody2d?
Here is the script by the way.
public class PlayerControll : MonoBehaviour
{
public float MoveForce = 10;
public float JumpForce = 6.3;
public float MaxVelocity = 5;
public LayerMask GroundLayer; //Ground is the layer mask
public float GroundDistance = 0.4;
void Update ()
{
if(rigidbody2D.isKinematic) return;
RaycastHit2D hit = Physics2D.Raycast(transform.position, new Vector2(0, -1), GroundDistance, GroundLayer);
bool grounded = hit.collider != null;
if (hit)
{
Debug.Log(hit.collider);
}
Debug.DrawRay(transform.position, new Vector2(0, -1), Color.red);
if(Input.GetKeyDown(KeyCode.Space) && grounded)
{
rigidbody2D.AddForce(new Vector2(0, JumpForce), ForceMode2D.Impulse);
}
if(Input.GetKey(KeyCode.LeftArrow) && rigidbody2D.velocity.x > -MaxVelocity)
{
rigidbody2D.AddForce(new Vector2(-MoveForce, 0));
}
if(Input.GetKey(KeyCode.RightArrow) && rigidbody2D.velocity.x < MaxVelocity)
{
rigidbody2D.AddForce(new Vector2(MoveForce, 0));
}
}
}