Skip to main content

添加力

跳起来

GetComponent<Rigidbody2D>().velocity = Vector2.zero;
GetComponent<Rigidbody2D>().AddForce(new Vector2(0, 12 * x), ForceMode2D.Impulse);
  • ForceMode2D.Impulse 瞬时的力
  • ForceMode2D.Force 持久的力

跳起来左右移动

            Vector3 acc = Vector3.zero;
Vector3 diff;
if (Input.GetKey(KeyCode.LeftArrow))
{
acc.x = -0.1f;
transform.localScale = new Vector3(-1, 1, 1);
}
if (Input.GetKey(KeyCode.RightArrow))
{
transform.localScale = new Vector3(1, 1, 1);
acc.x = 0.1f;
}

diff = Vector3.MoveTowards(transform.localPosition, transform.localPosition + acc, 0.5f * Time.time);
diff.y = transform.localPosition.y;
diff.z = 0;
//设置边界
if (diff.x < leftBorder)
{
diff.x = rightBorder;
}
if (diff.x > rightBorder)
{
diff.x = leftBorder;
}
transform.localPosition = diff;