Unity 2d弹跳离墙运动

时间:2018-07-26 08:53:31

标签: c# unity3d 2d

我正试图在现场移动一个球。球开始在随机位置移动,然后与包含Collider2D的对象碰撞,球改变其位置并向另一个方向移动。

  public float MoveSpeed;
private Rigidbody2D rb;

private void Start()
{
    rb = GetComponent<Rigidbody2D>();
    rb.velocity = RandomVector(-5f, 5f);
    transform.gameObject.GetOrAddComponent<ColliderEventSystem>().ColliderEntered += PushObject;
}

private void PushObject(ColliderEventSystem eventTarget, Collider2D other)
{
    float speed = 100;
    Vector3 direction = (transform.position - other.transform.position).normalized;
    rb.AddForce((direction * speed));
}

private Vector3 RandomVector(float min, float max)
{
    var x = UnityEngine.Random.Range(min, max);
    var y = UnityEngine.Random.Range(min, max);
    return new Vector3(x, y);
}

代码中有些错误,因为对象更改了位置,但没有按照预期移动。 enter image description here

图片显示了球应该如何运动!

0 个答案:

没有答案