我正试图在现场移动一个球。球开始在随机位置移动,然后与包含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);
}
图片显示了球应该如何运动!